androiduninstallationpackageinstaller

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


Solution

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