应用程序安装卸载监听-BroadcastRecevier

来源:互联网 发布:网络直播项目计划书 编辑:程序博客网 时间:2024/05/16 05:38
新建一个类继承BroadCastRecevier
package com.demo.clf;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.net.Uri;import android.widget.Toast;public class AppsRecevier extends BroadcastReceiver {@Overridepublic 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();}}}

注册广播

<receiver android:name="com.demo.clf.AppsRecevier">            <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>


0 0
原创粉丝点击