在Android项目中有时会需要用到如何监听到app是否安装或卸载,来做出相应的操作。整个过程也挺简单的
1.写一个类继承BroadcastReceiver
public class BootReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { <span style="font-family:微软雅黑;"> //安装广播监听</span> 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(); <pre name="code" class="java"> System.out.println("卸载了:" + packageName + "包名的程序"); } } }
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RESTART_PACKAGES" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".BootReceiver" android:label="@string/app_name" > <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>
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/xiaoxiaomao123098/article/details/47682095