Define a class derived from class BroadcastReceiver;
Register broadcast receiver; MyBroadcastReceiver myReceiver = new MyBroadcastReceiver(); IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_INSTALL); filter.addAction(Intent.ACTION_PACKAGE_REMOVED); filter.addAction(Intent.ACTION_PACKAGE_ADDED); filter.addAction(Intent.ACTION_PACKAGE_CHANGED); filter.addAction(Intent.ACTION_PACKAGE_RESTARTED); ... filter.addDataScheme("package"); //This line is very important. Otherwise, broadcast can't be received. registerReceiver(myReceiver, filter);
Notes: The package name is Intent.mData. Intent.mData is
not available in SDK 1.0, but it can be retrieved by calling
Intent.getDataString();
Uninstall package
Uninstall application in code Uri uri = Uri.formParts("package", , null); Intent intent = new Intent(Intent.ACTION_DELETE, uri); context.startActivity(intent);
Check if application is uninstalled successfully
Uninstall succeeded - Define a broadcast receiver to monitor Intent.ACTION_PACKAGE_REMOVED - Register such broadcast receiver
- Get result protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ) //resultCode always is 0, and the intent
argument 'data' is always null { //call packagemanager.getPackageInfo, return null if package is uninstalled } }
Note: This method is not invoked if the package is uninstalled successfully