概述
采用广播方式监听,可以纯代码
方式,也可以采用代码+XML
配置方式。下面分别就2种方法作介绍。
说明:本人安卓初学者,不保证代码一定没问题。
XML配置方式
首先在AndroidManifest.xml
中定义广播:
<receiver android:name=".ApkInstallReceiver" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<action android:name="android.intent.action.PACKAGE_CHANGED" />
<action android:name="android.intent.action.PACKAGE_RESTARTED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
然后再看ApkInstallReceiver:
public class ApkInstallReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
String packageName = intent.getData().getSchemeSpecificPart();
String action = intent.getAction();
String type = null;
if(Intent.ACTION_PACKAGE_ADDED.equals(action)) type = "added"; // 安装成功
else if(Intent.ACTION_PACKAGE_REMOVED.equals(action)) type = "removed"; // 卸载成功
else if(Intent.ACTION_PACKAGE_REPLACED.equals(action)) type = "replaced"; // 替换成功
else if(Intent.ACTION_PACKAGE_CHANGED.equals(action)) type = "changed"; // 应用被更改
else if(Intent.ACTION_PACKAGE_RESTARTED.equals(action)) type = "restarted"; // 应用重启
if(type != null) Log.i("info", "type:"+type+",packageName:"+packageName);
}
}
比较简单。
纯代码方式
先单独写一个类似工具类(当然也可以直接写在Activity里面):
public class ApkInstallListener
{
private Activity context;
public ApkInstallListener(){}
public ApkInstallListenerUtil(Activity context)
{
this.context = context;
}
BroadcastReceiver installListener = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String packageName = intent.getData().getSchemeSpecificPart();
String action = intent.getAction();
String type = null;
if(Intent.ACTION_PACKAGE_ADDED.equals(action)) type = "added"; // 安装成功
else if(Intent.ACTION_PACKAGE_REMOVED.equals(action)) type = "removed"; // 卸载成功
else if(Intent.ACTION_PACKAGE_REPLACED.equals(action)) type = "replaced"; // 替换成功
else if(Intent.ACTION_PACKAGE_CHANGED.equals(action)) type = "changed"; // 应用被更改
else if(Intent.ACTION_PACKAGE_RESTARTED.equals(action)) type = "restarted"; // 应用重启
if(type != null) Log.i("info", "type:"+type+",packageName:"+packageName);
}
};
public void register()
{
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
intentFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
intentFilter.addDataScheme("package");
context.registerReceiver(installListener, intentFilter);
}
public void unregister()
{
context.unregisterReceiver(installListener);
}
}
然后在Activity的onCreate
和onPause
里面分别调用注册和释放方法即可,需要特别注意的是一定不要忘了调用释放方法,否则会出现线程问题:
public class TestActivity extends Activity
{
// 省略其它代码
ApkInstallListener apkInstallListener;
@Override
protected void onCreate()
{
// 省略其它代码
apkInstallListener = new ApkInstallListener(this);
apkInstallListener.register(); // 注册apk事件监听
}
@Override
protected void onPause()
{
// 省略其它代码
if(apkInstallListener != null) apkInstallListener.unregister(); // 释放apk事件监听
}
}