Android apk安裝替換卸載廣播的實現(xiàn)代碼
首先是要獲取應用的安裝狀態(tài),通過廣播的形式以下是和應用程序相關(guān)的Broadcast Action
ACTION_PACKAGE_ADDED 一個新應用包已經(jīng)安裝在設備上,數(shù)據(jù)包括包名(最新安裝的包程序不能接收到這個廣播)
ACTION_PACKAGE_REPLACED 一個新版本的應用安裝到設備,替換之前已經(jīng)存在的版本
ACTION_PACKAGE_CHANGED 一個已存在的應用程序包已經(jīng)改變,包括包名
ACTION_PACKAGE_REMOVED 一個已存在的應用程序包已經(jīng)從設備上移除,包括包名(正在被安裝的包程序不能接收到這個廣播)
ACTION_PACKAGE_DATA_CLEARED 用戶已經(jīng)清楚一個包的數(shù)據(jù),包括包名(清除包程序不能接收到這個廣播)
代碼實現(xiàn)
在AndroidManifest.xml中定義廣播
<receiver android:name=".AppInstallReceiver"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
這里選用的是
ACTION_PACKAGE_ADDED 一個新應用包已經(jīng)安裝在設備上,數(shù)據(jù)包括包名(最新安裝的包程序不能接收到這個廣播)
ACTION_PACKAGE_REPLACED 一個新版本的應用安裝到設備,替換之前已經(jīng)存在的版本
ACTION_PACKAGE_REMOVED 一個已存在的應用程序包已經(jīng)從設備上移除,包括包名(正在被安裝的包程序不能接收到這個廣播)
再看AppInstallReceiver
public class AppInstallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PackageManager manager = context.getPackageManager();
if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
String packageName = intent.getData().getSchemeSpecificPart();
Toast.makeText(context, "安裝成功"+packageName, Toast.LENGTH_LONG).show();
}
if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {
String packageName = intent.getData().getSchemeSpecificPart();
Toast.makeText(context, "卸載成功"+packageName, Toast.LENGTH_LONG).show();
}
if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
String packageName = intent.getData().getSchemeSpecificPart();
Toast.makeText(context, "替換成功"+packageName, Toast.LENGTH_LONG).show();
}
}
}
代碼實現(xiàn)比較簡單,根據(jù)接收到的Action來判斷應用程序是安裝 卸載還是被替換成其他版本
相關(guān)文章
Android SharedPreferences實現(xiàn)保存登錄數(shù)據(jù)功能
這篇文章主要為大家詳細介紹了Android SharedPreferences實現(xiàn)保存登錄數(shù)據(jù)功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05Android Activity與Fragment實現(xiàn)底部導航器
這篇文章主要介紹了Android Activity與Fragment實現(xiàn)底部導航器的相關(guān)資料,并附實例代碼,需要的朋友可以參考下2016-11-11Android下拉刷新ListView——RTPullListView(demo)
下拉刷新已經(jīng)形成一種默認的用戶習慣,今天主要介紹下在Android上實現(xiàn)下拉刷新的Demo,感興趣的朋友可以參考下哈,希望可以幫助到你2013-04-04