Reputation: 904
The following code will uninstall a package on a device if I pass in a package name that exists on the phone.
public static String Execute(Context myContext, String packageName){
Intent intent = new Intent(Intent.ACTION_DELETE);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try{
intent.setData(Uri.parse("package:" + packageName));
myContext.startActivity(intent);
}catch(Exception e){
System.out.println("package " + packageName + " not installed");
return "Package was not uninstalled";
}
return "Package " + packageName + " uninstalled.";
}
However if I pass in a package name that isn't on the phone, my exception isn't handled and I get an error that says
"Invalid packageName or componentName in [packageName]"
and then the return statement "Package [packageName] uninstalled" is returned. What's wrong with my exception handling here? I want the code within the catch statement to be executed/returned if that error occurs, but it's not working.
Upvotes: 0
Views: 105
Reputation: 1006584
my exception isn't handled
That is because it is not an exception and is not coming from your process.
What's wrong with my exception handling here?
Nothing, other than it not being effective for this case.
Use PackageManager
to see if the package exists before attempting to delete it.
Upvotes: 2