Android編程監(jiān)聽APK安裝與刪除等過程的方法
本文實(shí)例講述了Android編程監(jiān)聽APK安裝與刪除等過程的方法。分享給大家供大家參考,具體如下:
軟件下載后的一系列動(dòng)作監(jiān)聽:先前是通過Service監(jiān)聽掃描獲取狀態(tài),以后用這個(gè)方法測試使用
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class getBroadcast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){ Toast.makeText(context, "有應(yīng)用被添加", Toast.LENGTH_LONG).show(); } else if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){ Toast.makeText(context, "有應(yīng)用被刪除", Toast.LENGTH_LONG).show(); } /* else if(Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())){ Toast.makeText(context, "有應(yīng)用被改變", Toast.LENGTH_LONG).show(); }*/ else if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){ Toast.makeText(context, "有應(yīng)用被替換", Toast.LENGTH_LONG).show(); } /* else if(Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())){ Toast.makeText(context, "有應(yīng)用被重啟", Toast.LENGTH_LONG).show(); }*/ /* else if(Intent.ACTION_PACKAGE_INSTALL.equals(intent.getAction())){ Toast.makeText(context, "有應(yīng)用被安裝", Toast.LENGTH_LONG).show(); }*/ } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="zy.Broadcast" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Broadcast" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="getBroadcast" android:enabled="true" > <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED"></action> <!-- <action android:name="android.intent.action.PACKAGE_CHANGED"></action>--> <action android:name="android.intent.action.PACKAGE_REMOVED"></action> <action android:name="android.intent.action.PACKAGE_REPLACED"></action> <!-- <action android:name="android.intent.action.PACKAGE_RESTARTED"></action>--> <!-- <action android:name="android.intent.action.PACKAGE_INSTALL"></action>--> <data android:scheme="package"></data> </intent-filter> </receiver> </application> <uses-sdk android:minSdkVersion="7" /> </manifest>
代碼實(shí)現(xiàn)添加:
private final BroadcastReceiver apkInstallListener = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){ System.out.println("**************Broadcase*************"); File file = uninstallApk.get(isDeleted); System.out.println(file.toString()+"*****"); file.delete(); //System.out.println(uninstallApk.size()+"(*******"+uApks.size()); if(uninstallApk!=null&&uApks!=null) { uninstallApk.remove(isDeleted); uApks.remove(isDeleted); } //清除集合里面的值 if(uninstallApk!=null) { System.out.println("onpause******"+uninstallApk.size()); uninstallApk.clear(); } if(uApks!=null) { uApks.clear(); } System.out.println("******應(yīng)用添加***"+isDeleted); Toast.makeText(context, "有應(yīng)用被添加"+isDeleted, Toast.LENGTH_LONG).show(); } else if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){ System.out.println("*****應(yīng)用被刪除"); Toast.makeText(context, "有應(yīng)用被刪除", Toast.LENGTH_LONG).show(); } /* else if(Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())){ Toast.makeText(context, "有應(yīng)用被改變", Toast.LENGTH_LONG).show(); }*/ else if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){ System.out.println("****應(yīng)用被替換"); Toast.makeText(context, "有應(yīng)用被替換", Toast.LENGTH_LONG).show(); } /* else if(Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())){ Toast.makeText(context, "有應(yīng)用被重啟", Toast.LENGTH_LONG).show(); }*/ /* else if(Intent.ACTION_PACKAGE_INSTALL.equals(intent.getAction())){ Toast.makeText(context, "有應(yīng)用被安裝", Toast.LENGTH_LONG).show(); }*/ } }; // 注冊(cè)監(jiān)聽 private void registerSDCardListener(){ IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED); intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED); intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED); intentFilter.addDataScheme("package"); registerReceiver(apkInstallListener, intentFilter); }
java里的調(diào)用 registerSDCardListener()
@Override protected void onDestroy() { super.onDestroy(); //unregisterReceiver(apkInstallListener); }
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android使用RSA加密和解密的示例代碼
- Android Studio時(shí)間選擇器的創(chuàng)建方法
- Android Studio實(shí)現(xiàn)帶邊框的圓形頭像
- Android ijkplayer的使用方法解析
- Android開發(fā)中使用WebView控件瀏覽網(wǎng)頁的方法詳解
- Android編程實(shí)現(xiàn)獲取當(dāng)前系統(tǒng)語言及地區(qū)并更改語言的方法
- Android實(shí)現(xiàn)單頁面浮層可拖動(dòng)view的一種方法
- Android:Field can be converted to a local varible.的解決辦法
相關(guān)文章
Android使用RecycleView實(shí)現(xiàn)拖拽交換item位置
這篇文章主要為大家詳細(xì)介紹了Android使用RecycleView實(shí)現(xiàn)拖拽交換item位置,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Android自定義view實(shí)現(xiàn)日歷打卡簽到
這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)日歷打卡簽到,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05Android筆記之:onConfigurationChanged詳解
本篇是對(duì)Android中onConfigurationChanged的使用進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05Android提高之SQLite分頁讀取實(shí)現(xiàn)方法
這篇文章主要介紹了Android的SQLite分頁讀取實(shí)現(xiàn)方法,在Android項(xiàng)目開發(fā)中非常實(shí)用,需要的朋友可以參考下2014-08-08Android?Flutter實(shí)現(xiàn)頁面切換轉(zhuǎn)場動(dòng)畫效果
Hero組件非常適合從列表、概覽頁切換到詳情頁轉(zhuǎn)場動(dòng)畫場合。本文將利用Hero組件制作一個(gè)簡單的頁面切換轉(zhuǎn)場動(dòng)畫效果,感興趣的可以了解一下2022-06-06Android實(shí)現(xiàn)讀取NFC卡卡號(hào)示例
本篇文章主要介紹了Android實(shí)現(xiàn)讀取NFC卡卡號(hào)示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01AndroidStudio3.6.1打包jar及AndroidStudio4.0打包jar的一系列問題及用法
這篇文章主要介紹了AndroidStudio3.6.1打包jar,AndroidStudio4.0打包jar的問題及用法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03Flutter?彈性布局基石flex算法flexible示例詳解
這篇文章主要為大家介紹了Flutter?彈性布局基石flex算法flexible示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Android SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄
這篇文章主要為大家詳細(xì)介紹了Android SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05