您的位置:首页 > 移动开发 > Android开发

Android 监听应用程序的安装、卸载

2016-11-01 00:00 357 查看

监听应用程序的安装、卸载

AndroidManifest.xml
中注册一个静态广播,监听安装的广播
android.intent.action.PACKAGE_ADDED
监听程序卸载的广播

android.intent.action.PACKAGE_REMOVED
,在广播中一定要加上
<data android:scheme="package" />


不然就监听不到

<receiver
android:name=".AppInstallReceiver" android:enabled="true">
<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>

在java代码中,需要写一个类继承
BroadcastReceiver


public class AppInstallReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(Intent.ACTION_PACKAGE_ADDED)){
Log.d("tag","app installed ");
}else if(action.equals(Intent.ACTION_PACKAGE_REMOVED)){
Log.d("tag","app uninstalled");
}
}

}

可以通过
intent
获取应用的包名

String pkgName = intent.getDataString().substring(8)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android apk 安装 卸载