Android technology notes-(install and uninstall an application broadcast)
Android technology notes-(install and uninstall an application broadcast)
This article summarizes the broadcast usage of installing and uninstalling apps in Android.
In the android system, the corresponding broadcast is sent during installation and uninstallation. After the application is installed, the android. intent. action. PACKAGE_ADDED broadcast is sent.
You can use intent. getDataString () to obtain the package name. When the program is uninstalled, the system sends android. intent. action. PACKAGE_REMOVED broadcast. Similarly, intent. getDataString () obtains the name of the uninstalled package. You can perform operations as needed.
Example:
Create a new BroadcastReceiver class, accept the corresponding broadcast, and handle it as needed.
Public class MyReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {// receive broadcast: run the program if (intent. getAction (). equals ("android. intent. action. BOOT_COMPLETED ") {Intent newIntent = new Intent (context, xxxActivity. class); newIntent. setAction ("android. intent. action. MAIN "); newIntent. addCategory ("android. intent. category. LAUNCHER "); newIntent. setFlags (Inte Nt. FLAG_ACTIVITY_NEW_TASK); context. startActivity (newIntent);} // receives broadcast: after an application package is installed on the device, if (intent. getAction (). equals ("android. intent. action. PACKAGE_ADDED ") {String packageName = intent. getDataString (). substring (8); System. out. println ("---------------" + packageName); Intent newIntent = new Intent (); newIntent. setClassName (packageName, packageName +. mainActivity "); newIntent. setAction (" Ndroid. intent. action. MAIN "); newIntent. addCategory ("android. intent. category. LAUNCHER "); newIntent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK); context. startActivity (newIntent);} // receives broadcast: An application package is deleted from the device. If (intent. getAction (). equals ("android. intent. action. PACKAGE_REMOVED ") {System. out. println ("********************************"); databaseHelper dbhelper = new DatabaseHelper (); dbhelper.exe cuteSql ("delete from xxx ");}}
Note:Modify the AndroidManifest. xml configuration file