Android Technology Point notes-(Install app and uninstall app broadcast)
Summary: Summarize the broadcast usage of apps installed and uninstalled in Android.
On Android, the installation and uninstallation will send out the corresponding broadcasts, and the system will broadcast Android.intent.action.PACKAGE_ADDED after the application installation is complete.
The installed package name can be obtained by intent.getdatastring (). The system sends Android.intent.action.PACKAGE_REMOVED broadcasts when the program is uninstalled. the same intent.getdatastring () gets the package name that was unloaded. According to the actual needs to do the corresponding operation.
Example:
Create a new Broadcastreceiver class, accept the broadcast and process it as required.
public class Myreceiver extends Broadcastreceiver {@Override public void OnReceive (Context Co ntext, Intent Intent) {//Receive broadcast: Run Program if (Intent.getaction (). Equals ("Android.intent.action.BOOT_COMPLETED") after system boot is complete {Intent newintent = new Intent (context, xxxactivity.class); Newintent.setaction ("Android.intent.action.MAIN"); Newintent.addcategory ("Android.intent.category.LAUNCHER"); Newintent.setflags (Intent.flag_activity_new_task); Context.startactivity (newintent); }//Receive broadcast: Automatically start the new installation application after a new application package is installed on the device if (Intent.getaction (). Equals ("Android.intent.action.PACKAGE_ADDED")) { & nbsp String PackageName = intent.getdatastring (). substring (8); SYSTEM.OUT.PRINTLN ("---------------" + PackageName);   Intent newintent = new Intent (); Newintent.setclassname (packagename,packagename+). Mainactivity "); newintent.setaction ("Android.intent.action.MAIN"); Newintent.addcatego Ry ("Android.intent.category.LAUNCHER"); Newintent.setflags ( Intent.flag_activity_new_task); context.startactivity (newintent); } //Receive broadcast: on device An application package was deleted. if (Intent.getaction () equals ("Android.intent.action.PACKAGE_REMOVED")) {    SYSTEM.OUT.PRINTLN ("********************************"); Databasehelper dbhelper = new Databasehelper (); Dbhelper.executesql ("delete from xxx"); }}
Note:Modifying the Androidmanifest.xml configuration file
<?xml version= "1.0" encoding= "UTF-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" App.ui "> <application> <receiver android:name=". Myreceiver "android:label=" @string/app_name "> <intent-filter> <action android:name= "Android.intent.action.BOOT_COMPLETED"/> <category androi D:name= "Android.intent.category.LAUNCHER"/> </intent-filter> <intent-filter> <action android:name= "Android.intent.action.PACKAGE_ADDED"/> <action Andro Id:name= "Android.intent.action.PACKAGE_REMOVED"/> <data android:scheme= "package"/> < ;! --Attention!! This sentence must be added, otherwise not receive broadcast--</intent-filter> </receiver> <activity A Ndroid:name= ". Xxxactivity" Android:labEl= "XXX" > <intent-filter> <action android:name= "android.intent.action. MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </ intent-filter> </activity> </application> <uses-permission android:name= "Android.per Mission. INTERNET "/> <uses-permission android:name=" Android.permission.RESTART_PACKAGES "/> <uses-p Ermission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED"/> </manifest>
Android Technology Point notes-(Install app and uninstall app broadcast)