有些時候,需要監聽到程式是否安裝或者卸載成功,在android系統中,安裝和卸載都會發送廣播,當應用安裝完成後系統會發android.intent.action.PACKAGE_ADDED廣播。可以通過intent.getDataString()獲得所安裝的包名。當卸載程式時系統發android.intent.action.PACKAGE_REMOVED廣播。同樣intent.getDataString()獲得所卸載的包名。
所以,只需要自訂一個BroadcastReceiver,來對系統廣播進行監聽和處理。
1、自訂廣播
自訂廣播MyInstalledReceiver繼承自BroadcastReceiver,實現其onReceive()方式,具體代碼如下:
[html]
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent){
//接收安裝廣播
if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
String packageName = intent.getDataString();
System.out.println("安裝了:" +packageName + "包名的程式");
}
//接收卸載廣播
if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {
String packageName = intent.getDataString();
System.out.println("卸載了:" + packageName + "包名的程式");
}
}
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent){
//接收安裝廣播
if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
String packageName = intent.getDataString();
System.out.println("安裝了:" +packageName + "包名的程式");
}
//接收卸載廣播
if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {
String packageName = intent.getDataString();
System.out.println("卸載了:" + packageName + "包名的程式");
}
}
} 2、註冊監聽
1) xml 方式:在AndroidManifest.xml 設定檔的Application節點下,添加自訂的註冊監聽 MyInstalledReceiver,在AndroidManifest.xml 添加的註冊監聽,其生命週期預設是整個應用的生命週期。
[html]
<?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>
<?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) 代碼方式:一般在Activity的onStart()方法中註冊監聽,在onDestroy()方法中登出監聽(也可以在onStop()方法中登出,其生命週期登出時結束)。
[html]
@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();
}
@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();
} 3、儲存安裝、卸載資訊
一般時候,需要使用到程式的安裝、卸載資訊,所以需要將資訊儲存起來,比較好的方式是使用Sqlite資料庫,也可以使用喜好設定等。