I have the following broadcast receiver to capture the event when user is uninstalling app on device, technically, I am receiving intent with action ACTION_PACKAGE_REMOVED:
public class appUninstallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
if (intent != null) {
if (intent.getAction().equals(intent.ACTION_PACKAGE_REMOVED)) {
try {
String packageName = intent.getData().toString();
//Logcat shows the packageName is "com.XXX.YYY"
Log.v("debug",packageName);
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
//Got NameNotFoundException
Log.v("debug",packageInfo.versionName);
}catch(NameNotFoundException e){
e.printStackTrace();
}
}
}
}
}
The above receiver works well except that when it tries to extract the version name of the uninstalling app(package) with packageInfo.versionName, the NameNotFoundException is rising.
The packageName I got is "com.XXX.YYY" which is exactly the package name of the app I am uninstalling. But why I am not able to get the version name with above code?
(By the way, the above receiver is triggered when app uninstall starts, is it because the system has already removed the metadata before it starts uninstallation?)
But why I am not able to get the version name with above code?
You will notice that the action name (ACTION_PACKAGE_REMOVED) is past tense. The action name is not ACTION_PACKAGE_REMOVING, ACTION_PACKAGE_WILL_BE_REMOVED_IN_THE_FUTURE, or ACTION_PACKAGE_USER_WANTS_TO_REMOVE_THIS_PACKAGE_WHICH_WE_WILL_DO_ANY_MINUTE_NOW.
The documentation also uses the past tense:
An existing application package has been removed from the device.
Hence, if the application is being uninstalled, the application does not exist on the device by the time this broadcast is sent, and therefore it will not be available via PackageManager.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With