简体   繁体   中英

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. 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 . 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. Just change your Intent action to

Intent.ACTION_DELETE

Also you need to add the permission to delete packages as well.

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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