Android實踐 -- 監聽應用程序的安裝、卸載


監聽應用程序的安裝、卸載

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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM