简体   繁体   中英

How to uninstall apps in android programmatically with PackageInstaller

I have created a simple android uninstaller app that helps to remove multiple apps at once. I use this code to accomplish this task:

Uri uri = Uri.fromParts("package", app.getPackageName(), null);
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, uri);
// store result
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
startActivityForResult(intent, 1);

But now, according to this android doc , ACTION_UNINSTALL_PACKAGE is deprecated in API level 29 and it's recommended to use PackageInstaller.uninstall(String, IntentSender) instead. I want to use this new api but I can't find any good example on how this is implemented. Any help is appreciated thanks.

You can refer to this link

https://www.programcreek.com/java-api-examples/index.php?api=android.content.pm.PackageInstaller

and implement like this -

@Override
public void uninstall(String packageName, String callerPackageName, int flags, IntentSender statusReceiver, int userId) throws RemoteException {
boolean success = VAppManagerService.get().uninstallPackage(packageName);
if (statusReceiver != null) {
    final Intent fillIn = new Intent();
    fillIn.putExtra(PackageInstaller.EXTRA_PACKAGE_NAME, packageName);
    fillIn.putExtra(PackageInstaller.EXTRA_STATUS, success ? PackageInstaller.STATUS_SUCCESS : PackageInstaller.STATUS_FAILURE);
    fillIn.putExtra(PackageInstaller.EXTRA_STATUS_MESSAGE, PackageHelper.deleteStatusToString(success));
    fillIn.putExtra("android.content.pm.extra.LEGACY_STATUS", success ? 1 : -1);
    try {
        statusReceiver.sendIntent(mContext, 0, fillIn, null, null);
    } catch (IntentSender.SendIntentException e) {
        e.printStackTrace();
    }
  }
}

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