简体   繁体   English

如何使用新的 PackageInstaller api 卸载 android 应用程序?

[英]How to uninstall android apps using the new PackageInstaller api?

The old way of uninstalling android apps with ACTION_UNINSTALL_PACKAGE is deprecated in API level 29. Now it's recommended to use PackageInstaller.uninstall(packageName: String, statusReceiver: IntentSender) instead.使用ACTION_UNINSTALL_PACKAGE卸载 android 应用程序的旧方法在 API 级别 29 中已弃用。现在建议使用PackageInstaller.uninstall(packageName: String, statusReceiver: IntentSender)代替。 This is what a came-up with so far:到目前为止,这是一个想法:

fun uninstal(){
    val packageName = "some package name"
    val packageInstaller = this.packageManager.packageInstaller
    val intent = Intent(this, this::class.java)
    val sender = PendingIntent.getActivity(this, 0, intent, 0)
    packageInstaller.uninstall(packageName, sender.intentSender) 
}

I cannot figure out how to provide the IntentSender .我不知道如何提供IntentSender I tried to make an intent from and to the current activity but all this code does is recreate the activity.我试图在当前活动中创建一个意图,但是这些代码所做的只是重新创建活动。 Any idea please?请问有什么想法吗? and thanks谢谢

The Intent based method still works on API Level 29+ devices.基于 Intent 的方法仍然适用于 API Level 29+ 设备。 Just change your Intent action to只需将您的 Intent 操作更改为

Intent.ACTION_DELETE意图.ACTION_DELETE

Also you need to add the permission to delete packages as well.此外,您还需要添加删除包的权限。

<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES"/> <uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES"/>

Here is the complete code:这是完整的代码:

val pkg             = "package_to_delete" 
val uri: Uri        = Uri.fromParts("package", pkg, null)
val uninstallIntent = Intent(Intent.ACTION_DELETE, uri)

startActivityForResult(uninstallIntent, EXIT_REQUEST)

In the above code, pkg is the packageName of the App you want to delete in string format and EXIT_REQUEST is an Integer value.在上面的代码中,pkg 是您要删除的应用程序的 packageName 字符串格式,EXIT_REQUEST 是一个 Integer 值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM