Skip to content

Instantly share code, notes, and snippets.

@davidelp68
davidelp68 / MainActivity_WV11_2.kt
Created Aug 13, 2021
Android Studio - WebView
View MainActivity_WV11_2.kt
package com.dm.tutorialwebview11
import android.content.Context //Importazione della Classe: Context
import android.net.ConnectivityManager //Importazione della Classe: ConnectivityManager
import android.net.NetworkCapabilities //Importazione della Classe: NetworkCapabilities
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.WebChromeClient
import android.webkit.WebView
@davidelp68
davidelp68 / MainActivity_WV11.kt
Created Aug 13, 2021
Android Studio - WebView
View MainActivity_WV11.kt
if(isConnected(this))
{
//aggiungere qui eventuale codice da eseguire se il dispositivo è connesso ad internet
}
else
{
//aggiungere qui eventuale codice da eseguire se il dispositivo NON è connesso ad internet
Toast.makeText(this, "Non connesso ad internet", Toast.LENGTH_LONG).show()
}
@davidelp68
davidelp68 / check_connection2.kt
Created Aug 13, 2021
Android Studio - WebView
View check_connection2.kt
private fun isConnected(context: Context): Boolean
{
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
////per versioni Android uguali o superiori ad Android M (Android 6.0 - API 23)
val nw = connectivityManager.activeNetwork ?: return false
val actNw = connectivityManager.getNetworkCapabilities(nw) ?: return false
return when {
actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
View AndroidManifest_WV11.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dm.tutorialwebview11">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
View MainActivity_permission7.kt
package com.dm.tutorialpermission7
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import android.view.View
import android.widget.Toast
@davidelp68
davidelp68 / permission_write7_b.kt
Created Sep 6, 2020
Android Studio - Permessi
View permission_write7_b.kt
//funzione che recupera la scelta fatta nei permessi
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String?>, grantResults: IntArray) {
if (grantResults.isNotEmpty() && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
//inserire qui il codice da eseguire se i permessi sono concessi
Toast.makeText(this, "Premessi concessi", Toast.LENGTH_SHORT).show()
}
else
{
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
@davidelp68
davidelp68 / permission_write7.kt
Created Sep 6, 2020
Android Studio - Permessi
View permission_write7.kt
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
@davidelp68
davidelp68 / MainActivity_screenshot2.kt
Created Aug 30, 2020
Android Studio - Take Screenshot
View MainActivity_screenshot2.kt
package com.dm.tutorialscreenshot2
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.os.Environment
@davidelp68
davidelp68 / take_save_screen.kt
Created Aug 30, 2020
Android Studio - Take Screenshot
View take_save_screen.kt
//chiamata dell'object Screenshot
val b: Bitmap = Screenshot.takescreenshotOfRootView(imageview)
imageView!!.setImageBitmap(b)
//questa riga serve per far vedere bene l'immagine acquisita
mainL!!.setBackgroundColor(Color.parseColor("#999999"))
//*****************************************************
//chiamata alla funzione saveScreenshot con passaggio immagine creata
saveScreenshot(b)
@davidelp68
davidelp68 / saveScreenshot.kt
Created Aug 30, 2020
Android Studio - Take Screenshot
View saveScreenshot.kt
//funzione che esegue il salvataggio nella cartella Download del dispositivo dell'immagine creata e la sua visualizzazione
private fun saveScreenshot(bitmap: Bitmap) {
val filePath = Environment.getExternalStorageDirectory().toString() + "/Download/" + Calendar.getInstance().time.toString() + ".jpg"
val fileScreenshot = File(filePath)
val fileOutputStream: FileOutputStream?
try {
fileOutputStream = FileOutputStream(fileScreenshot)
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream)
fileOutputStream.flush()
fileOutputStream.close()