Android 應(yīng)用程序的安裝和卸載事件,,是由系統(tǒng)進(jìn)行監(jiān)聽(tīng)并全局廣播的,,支持1.5(android 3)以上 因此,如果想要監(jiān)聽(tīng)獲取應(yīng)用的安裝和卸載事件,,只需要自定義一個(gè)BroadcastReceiver,,來(lái)對(duì)系統(tǒng)廣播進(jìn)行監(jiān)聽(tīng)和處理
BroadcastReceiver 是系統(tǒng)全局廣播監(jiān)聽(tīng)類, 其主要方法是onReceive(),,自定義的廣播類繼承于它并實(shí)現(xiàn)自己的onReceive()處理邏輯
BroadcastReceiver 使用前,,需要進(jìn)行注冊(cè)監(jiān)聽(tīng)(xml和代碼兩種方式),不使用時(shí)需要注銷監(jiān)聽(tīng),,其生命周期一般為整個(gè)應(yīng)用的生命周期
1,, 自定義廣播 自定義廣播MyInstalledReceiver繼承自BroadcastReceiver,實(shí)現(xiàn)其onReceive()方式,,具體代碼如下: - public class MyInstalledReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
-
- if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) { // install
- String packageName = intent.getDataString();
-
- Log.i("homer", "安裝了 :" + packageName);
- }
-
- if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) { // uninstall
- String packageName = intent.getDataString();
-
- Log.i("homer", "卸載了 :" + packageName);
- }
- }
- }
2,, 注冊(cè)監(jiān)聽(tīng) 1) xml 方式 在AndroidManifest.xml 配置文件的Application節(jié)點(diǎn)下,添加自定義的注冊(cè)監(jiān)聽(tīng) MyInstalledReceiver - <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas./apk/res/android"
- package="com.homer.installed"
- 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>
在AndroidManifest.xml 添加的注冊(cè)監(jiān)聽(tīng),,其生命周期默認(rèn)是整個(gè)應(yīng)用的生命周期
2) 代碼方式 一般在Activity的onStart()方法中注冊(cè)監(jiān)聽(tīng),,在onDestroy()方法中注銷監(jiān)聽(tīng)(也可以在onStop()方法中注銷,其生命周期注銷時(shí)結(jié)束) - @Override
- public 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);
- }
-
- @Override
- public void onDestroy(){
- if(installedReceiver != null) {
- this.unregisterReceiver(installedReceiver);
- }
-
- super.onDestroy();
- }
以上xml和代碼兩種注冊(cè)方式,,使用時(shí)選擇其一即可,; 如果同時(shí)使用兩種方式,,則兩種方式都有效,,即一次安裝或卸載均統(tǒng)計(jì)了兩次(重復(fù)統(tǒng)計(jì))
3, 結(jié)果測(cè)試
源碼下載
參考推薦: Receiving package install and uninstall events
Android BroadcastReceiver(推薦)
Android Manifest.xml 結(jié)構(gòu)詳解
Android Intent Action 大全
intent(google)
|