安卓通过代码安装应用和卸载应用
2023-02-12 本文已影响0人
蓝不蓝编程
卸载应用
- 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)
}