In some cases, you need to monitor whether the program is successfully installed or uninstalled. In the Android system, both installation and uninstallation will send a broadcast. After the application is installed, the system will send a broadcast to Android. intent. action. package_added broadcast. 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.
Therefore, you only need to customize a broadcastreceiver to listen to and process system broadcasts.
1. Custom Broadcast
Custom broadcast myinstalledreceiver inherits from broadcastreceiver and implements its onreceive () method. The Code is as follows:
Import android. content. broadcastreceiver; import android. content. context; import android. content. intent; public class bootreceiver extends broadcastreceiver {@ override public void onreceive (context, intent) {// receives and installs broadcast if (intent. getaction (). equals ("android. intent. action. package_added ") {string packagename = intent. getdatastring (); system. out. println ("installed:" + packagename + "package name program");} // receives the unmounted broadcast if (intent. getaction (). equals ("android. intent. action. package_removed ") {string packagename = intent. getdatastring (); system. out. println ("uninstalled:" + packagename + "package name program ");}}}
2. Register a listener
1) XML Method: In androidmanifest. under the application node of the XML configuration file, add the custom registration listener myinstalledreceiver in androidmanifest. the life cycle of the registration listener added in XML is the life cycle of the entire application by default.
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.apk" android:versionCode="1" android:versionName="1.0" > <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".MyInstalledReceiver" > <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <action android:name="android.intent.action.PACKAGE_REMOVED" /> <data android:scheme="package" /> </intent-filter> </receiver> </application> <uses-sdk android:minSdkVersion="3" /></manifest>
2) code method: Generally, the listener is registered in the onstart () method of the activity, and the listener can be deregistered in the ondestroy () method (or in the onstop () method, end of lifecycle cancellation ).
@Overridepublic void onStart(){super.onStart();installedReceiver = new MyInstalledReceiver();IntentFilter filter = new IntentFilter();filter.addAction("android.intent.action.PACKAGE_ADDED");filter.addAction("android.intent.action.PACKAGE_REMOVED");filter.addDataScheme("package");this.registerReceiver(installedReceiver, filter);}@Overridepublic void onDestroy(){if(installedReceiver != null) {this.unregisterReceiver(installedReceiver);}super.onDestroy();}
3. Store installation and uninstallation Information
Generally, you need to use the installation and uninstallation information of the program, so you need to store the information. A better way is to use the SQLite database, or you can use preferences.
Read: http://www.cnblogs.com/stoic/archive/2012/07/11/2586963.html
Http://blog.csdn.net/sunny2come/article/details/6324965
Http://blog.csdn.net/wangjia55/article/details/8470611
Http://blog.csdn.net/zeng622peng/article/details/8103396
Http://blog.csdn.net/sunboy_2050/article/details/8240372
Http://www.cnblogs.com/rayray/p/3178403.html