4

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

Toni Joe
  • 7,715
  • 12
  • 50
  • 69
  • "I tried to make an intent from and to the current activity but all this code does is recreate the activity" -- then perhaps use `getBroadcast()` and route to a `BroadcastReceiver`. We do not know what you are looking to do when the uninstall operation is finished. – CommonsWare Apr 26 '20 at 14:50
  • Well normally when one try to uninstall an app, a popup window appears prompting to user whether to uninstall the app or to cancel but in this case nothing happens – Toni Joe Apr 26 '20 at 14:55
  • I think that `IntentSender` is for the end result, not for the user confirmation. The user confirmation would be raised by the OS. Do you have permission to uninstall packages? – CommonsWare Apr 26 '20 at 15:00
  • Yes, I added the necessary permissions as specified in the documentation but still nothing happens – Toni Joe Apr 26 '20 at 15:12

1 Answers1

2

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.