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?)