首先创建一个类继承BroadcastReceiver,然后配置Manifest.xml
1 <receiver android:name=".PackageAddRemove"> 2 <intent-filter> 3 <action android:name="android.intent.action.PACKAGE_REMOVED"/> 4 <action android:name="android.intent.action.PACKAGE_ADDED"/> 5 <data android:scheme="package"/> 6 </intent-filter> 7 </receiver>
然后在创建的类里面写一些自己想写的逻辑代码
1 public class PackageAddRemove extends BroadcastReceiver { 2 @Override 3 public void onReceive(Context context, Intent intent) { 4 String action = intent.getAction(); 5 if (action.equals("android.intent.action.PACKAGE_REMOVED")){ 6 System.out.println("卸载"); 7 }else if (action.equals("android.intent.action.PACKAGE_ADDED")){ 8 System.out.println("安装"); 9 } 10 } 11 }