Android编程实现监控apk安装,卸载,替换的方法
本文实例讲述了Android编程实现监控apk安装,卸载,替换的方法。分享给大家供大家参考,具体如下:
publicclassGetBroadcastextendsBroadcastReceiver{ privatestaticGetBroadcastmReceiver=newGetBroadcast(); privatestaticIntentFiltermIntentFilter; publicstaticvoidregisterReceiver(Contextcontext){ mIntentFilter=newIntentFilter(); mIntentFilter.addDataScheme("package"); mIntentFilter.addAction(Intent.ACTION_PACKAGE_ADDED); mIntentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); mIntentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED); context.registerReceiver(mReceiver,mIntentFilter); } publicstaticvoidunregisterReceiver(Contextcontext){ context.unregisterReceiver(mReceiver); } @Override publicvoidonReceive(Contextcontext,Intentintent){ Stringaction=intent.getAction(); if(Intent.ACTION_PACKAGE_ADDED.equals(action)){ Toast.makeText(context,"有应用被添加",Toast.LENGTH_LONG).show(); }elseif(Intent.ACTION_PACKAGE_REMOVED.equals(action)){ Toast.makeText(context,"有应用被删除",Toast.LENGTH_LONG).show(); } /* *elseif(Intent.ACTION_PACKAGE_CHANGED.equals(action)){ *Toast.makeText(context,"有应用被改变",Toast.LENGTH_LONG).show();} */ elseif(Intent.ACTION_PACKAGE_REPLACED.equals(action)){ Toast.makeText(context,"有应用被替换",Toast.LENGTH_LONG).show(); } /* *elseif(Intent.ACTION_PACKAGE_RESTARTED.equals(action)){ *Toast.makeText(context,"有应用被重启",Toast.LENGTH_LONG).show();} */ /* *elseif(Intent.ACTION_PACKAGE_INSTALL.equals(action)){ *Toast.makeText(context,"有应用被安装",Toast.LENGTH_LONG).show();} */ } }
希望本文所述对大家Android程序设计有所帮助。