Android代码如何监控apk安装 卸载 替换

public class GetBroadcast extends BroadcastReceiver {
02  
03 private static GetBroadcast mReceiver = new GetBroadcast();
04 private static IntentFilter mIntentFilter;
05  
06  
07 public static void registerReceiver(Context context) {
08 mIntentFilter = new IntentFilter();
09 mIntentFilter.addDataScheme("package");
10 mIntentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
11 mIntentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
12 mIntentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
13 context.registerReceiver(mReceiver, mIntentFilter);
14 }
15  
16 public static void unregisterReceiver(Context context) {
17 context.unregisterReceiver(mReceiver);
18 }
19  
20 @Override
21 public void onReceive(Context context, Intent intent) {
22 String action = intent.getAction();
23 if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
24 Toast.makeText(context, "有应用被添加", Toast.LENGTH_LONG).show();
25 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
26 Toast.makeText(context, "有应用被删除", Toast.LENGTH_LONG).show();
27 }
28  
29 /*
30 * else if(Intent.ACTION_PACKAGE_CHANGED.equals(action)){
31 * Toast.makeText(context, "有应用被改变", Toast.LENGTH_LONG).show(); }
32 */
33  
34 else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
35 Toast.makeText(context, "有应用被替换", Toast.LENGTH_LONG).show();
36 }
37  
38 /*
39 * else if(Intent.ACTION_PACKAGE_RESTARTED.equals(action)){
40 * Toast.makeText(context, "有应用被重启", Toast.LENGTH_LONG).show(); }
41 */
42  
43 /*
44 * else if(Intent.ACTION_PACKAGE_INSTALL.equals(action)){
45 * Toast.makeText(context, "有应用被安装", Toast.LENGTH_LONG).show(); }
46 */
47 }
48  
49 }

你可能感兴趣的:(Android代码如何监控apk安装 卸载 替换)