1: einen Anruf tätigen
val uri = Uri.parse("tel:10086")
val intent = Intent(Intent.ACTION_DIAL,uri)
startActivity(intent)
2: SMS senden
val smsUri = Uri.parse("smsto:10086")
val intent1 = Intent(Intent.ACTION_SENDTO,smsUri)
intent1.putExtra("sms_body","Hello")
startActivity(intent1)
3: Öffnen Sie den Browser
val webViewUri = Uri.parse("https://www.baidu.com")
val intent2 = Intent(Intent.ACTION_VIEW,webViewUri)
startActivity(intent2)
4: Multimedia öffnen, Musik abspielen
// /storage/emulated/0/Music/foo.mp3
val file = Environment.getExternalStoragePublicDirectory("Music/foo.mp3")
Log.d(Companion.TAG,file.path)
Log.d(Companion.TAG,file.name)
val intent = Intent(Intent.ACTION_VIEW)
val audio = FileProvider.getUriForFile(this, "$packageName.fileProvider",file)
Log.d(Companion.TAG,audio.path.toString())
intent.setDataAndType(audio,"audio/mp3")
startActivity(intent)
Höhere Versionen von Android können Dateien nicht direkt abrufen und müssen ContentProvide verwenden, um einen temporären Dateipfad zu erstellen
Verursacht durch: android.os.FileUriExposedException: file:///storage/emulated/0/Music/foo.mp3 wird durch Intent.getData() über die App hinaus verfügbar gemacht
Die Methode zur Verwendung von ContentProvide zum Erstellen eines temporären Dateipfads:
1: Anbieter in Manifest.xml deklarieren
<provider android:name="androidx.core.content.FileProvider" android:authorities="${application}.fileProvider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/> </provider>
2: res/xml Erstellen Sie eine neue Datei „provider_paths.xml“. Der Inhalt lautet wie folgt
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path path="." name="external_storage_root" /> </paths>
Achten Sie auf den Wert des Namens
Verursacht durch: java.lang.IllegalArgumentException: Das konfigurierte Stammverzeichnis konnte nicht gefunden werden
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path path="." name="external_storage_root" /> <!-- 对应Environment.getExternalStorageDirectory() --> <cache-path name="cache_files" path="."/> <!-- 对应应用程序内部存储区域的cache子目录中的文件Context.getCacheDir() --> <external-files-path name="external_files" path="."/> <!-- 对应应用程序外部存储区根目录中的文件Context.getExternalFilesDir(null) --> <external-cache-path name="external_cache_files" path="."/> <!-- 对应应用程序外部缓存区域根目录中的文件Context.getExternalCacheDir() --> <files-path name="path_files" path="."/> <!-- 对应应用程序内部存储区域的子目录中的文件Context.getFilesDir() --> <external-media-path name="external_media_path" path="."/><!-- 对应代表应用程序外部媒体区域根目录中的文件Context.getExternalMediaDirs() --> <root-path path="." name="external_999_root" /> <!-- 对应SD卡 --> </paths>
5: Schalten Sie die Kamera ein
//打开相机
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
//务必传值,不然onActivityResult照片返回值data为null
intent.putExtra(MediaStore.EXTRA_OUTPUT,savePhoto())
val launcherActivityInfo = registerForActivityResult(ActivityResultContracts.
StartActivityForResult()
) {
if (it != null) {
if (intent.clipData!=null) {
for (i in 0 until intent.clipData!!.itemCount) {
val uri = intent.clipData!!.getItemAt(i).uri
Log.d(TAG, "multiple current Uri:$uri")
}
}
}
}
launcherActivityInfo.launch(intent)
private fun savePhoto():Uri{
val outPath = Environment.getExternalStorageDirectory()
.absoluteFile.absolutePath+"/poo.jpg"
val file = File(outPath)
val audio = FileProvider.getUriForFile(this, "$packageName.fileProvider",file)
Log.d(TAG,audio.path.toString())
return audio
}
6: Holen Sie sich das Album und schneiden Sie es zu
// 获取相册并裁剪图片
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "image/*"
intent.putExtra("crop","true")
intent.putExtra("aspectX",1)
intent.putExtra("aspectY",2)
intent.putExtra("outputX",20)
intent.putExtra("outputY",40)
intent.putExtra("output",savePhoto())
intent.putExtra("outputFormat","JPEG")
val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it != null) {
if(it.data!=null){
Log.d(TAG,it.data!!.data.toString())
Glide.with(this).load(it.data!!.data).into(iamge)
}
}
}
launcher.launch(intent)
7: Systemeinstellungen öffnen
//进入系统设置页面
val intent = Intent(android.provider.Settings.ACTION_SETTINGS)
val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
}
launcher.launch(intent)
8: Öffnen Sie die Einstellungsseite für das drahtlose Netzwerk des Systems
//打开系统无线网络设置页面
val intent = Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS)
val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
}
launcher.launch(intent)