卸载应用
- AndroidManifest.xml增加权限
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
- 调用代码
fun uninstallAPK(apkPackageName: String) {
val intent = Intent(Intent.ACTION_DELETE)
intent.data = Uri.parse("package:$apkPackageName")
startActivity(intent)
}
安装应用
- AndroidManifest.xml增加权限
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
- 调用代码
fun install(context: Context, apkFilePath: String) {
val intent = Intent(Intent.ACTION_VIEW)
val file = File(apkFilePath)
val uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".FileProvider", file)
val type = "application/vnd.android.package-archive"
intent.setDataAndType(uri, type)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
context.startActivity(intent)
}
网友评论