1:拨打电话
val uri = Uri.parse("tel:10086")
val intent = Intent(Intent.ACTION_DIAL,uri)
startActivity(intent)
2:发送短信
val smsUri = Uri.parse("smsto:10086")
val intent1 = Intent(Intent.ACTION_SENDTO,smsUri)
intent1.putExtra("sms_body","Hello")
startActivity(intent1)
3:打开浏览器
val webViewUri = Uri.parse("https://www.baidu.com")
val intent2 = Intent(Intent.ACTION_VIEW,webViewUri)
startActivity(intent2)
4:打开多媒体,播放音乐
// /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)
高版本安卓无法直接获取file,需要使用ContentProvide建立临时文件路径
Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/Music/foo.mp3 exposed beyond app through Intent.getData()
使用ContentProvide 建立临时文件路径的方法:
1:在Manifest.xml里声明provider
<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 新建provider_paths.xml文件,内容如下
<?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>
注意name的值文章来源:https://www.toymoban.com/news/detail-620899.html
Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains文章来源地址https://www.toymoban.com/news/detail-620899.html
<?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:打开相机
//打开相机
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:获取相册并裁剪
// 获取相册并裁剪图片
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:打开系统设置
//进入系统设置页面
val intent = Intent(android.provider.Settings.ACTION_SETTINGS)
val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
}
launcher.launch(intent)
8:打开系统无线网络设置页面
//打开系统无线网络设置页面
val intent = Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS)
val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
}
launcher.launch(intent)
到了这里,关于安卓Intent打开系统进程汇总的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!