How can I detect that an application is being deleted?

I am going to remove the application when the user clicks a button. with this code:

Uri packageURI = Uri.parse("package:" + pkNames[position]); Intent uninstallIntent = new Intent( Intent.ACTION_DELETE, packageURI); context.startActivity(uninstallIntent); 

but some applications are not uninstalled. for example Setting or Music or ... when I am going to uninstall these applications, I see: uninstall failed.

I get my packages using this code:

  Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); packages = pm.queryIntentActivities(mainIntent, 0); 

How can I detect if an application has been deleted or not?

+4
source share
1 answer

You should check if the application you are trying to uninstall is a β€œsystem” by looking at ApplicationInfo.flags . The system application has the bit ApplicationInfo.FLAG_SYSTEM . Here is a small piece of code:

 boolean isSystem(ApplicationInfo info) { return (info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; } 

Check the documentation for the ApplicationInfo class for other useful flags.

+3
source

Source: https://habr.com/ru/post/1437025/


All Articles