Delete my application programmatically (Android)

前端 未结 3 1742
暗喜
暗喜 2020-12-09 11:08

I want to uninstall my application on button click. For this I am using following code.

Uri packageURI = Uri.parse(\"package:\"+packageName);
Intent uninstal         


        
3条回答
  •  感情败类
    2020-12-09 11:40

    You should first look into the Android native PackageInstaller. I would recommendating you to update all the code you use.


    Next step is to inspect PackageInstaller which is an normal class. You will find that uninstall function there. The bad news is that this needs Manifest.permission.DELETE_PACKAGES permission and its only granted to system apps. This means that this is not available directly to other developers. But we can access it using device owner permission.


    This requires:

    • Android 6.0 or newer
    • Device owner permission to uninstall the package

    Generally the DELETE_PACKAGES permission says:

    Allows an application to delete packages.

    Not for use by third-party applications.

    Once your app gets the device owner permission, you can uninstall an package like this:

    String appPackage = "com.your.app.package";
    Intent intent = new Intent(getApplicationContext(), 
    getApplicationContext().getClass()); //getActivity() is undefined!
    PendingIntent sender = PendingIntent.getActivity(getActivity(), 0, intent, 0);
    PackageInstaller mPackageInstaller = 
    getActivity().getPackageManager().getPackageInstaller();
    mPackageInstaller.uninstall(appPackage, sender.getIntentSender());
    

    The code used available here:

    PackageInstaller "Silent install and uninstall of apps by Device Owner” - Android M Preview

提交回复
热议问题