应用的安装卸载更新监听

第一步,创建一个广播接收者,

 AppsReceiver extends BroadcastReceiver
第二步,在AndroidManifest.xml配置文件中注册广播接收者与配置应用状态发生时对应的Action:
 <receiver android:name="com.itheima.appslistener.AppsReceiver">
            <intent-filter >
                <action android:name="android.intent.action.PACKAGE_ADDED"/>
                <action android:name="android.intent.action.PACKAGE_REPLACED"/>
                <action android:name="android.intent.action.PACKAGE_REMOVED"/>
                <data android:scheme="package"/>
            </intent-filter>
        </receiver>
第三步,重写广播接受者中OnReceiver方法:
public void onReceive(Context context, Intent intent) {
//判断收到的是什么广播
String action = intent.getAction();
//获取安装更新卸载的是什么应用
Uri uri = intent.getData();
if(Intent.ACTION_PACKAGE_ADDED.equals(action)){
Toast.makeText(context, uri + "被安装了", 0).show();
}
else if(Intent.ACTION_PACKAGE_REMOVED.equals(action)){
Toast.makeText(context, uri + "被删除了", 0).show();
}
else if(Intent.ACTION_PACKAGE_REPLACED.equals(action)){
Toast.makeText(context, uri + "被更新了", 0).show();
}
 }
 
 
posted @ 2017-02-13 13:21  SoulCode  阅读(499)  评论(0编辑  收藏  举报