您好,登錄后才能下訂單哦!
這篇文章主要介紹Android如何監(jiān)聽APK安裝與刪除等過程,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
具體如下:
軟件下載后的一系列動作監(jiān)聽:先前是通過Service監(jiān)聽掃描獲取狀態(tài),以后用這個方法測試使用
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>
代碼實現(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(); }*/ } }; // 注冊監(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); }
以上是“Android如何監(jiān)聽APK安裝與刪除等過程”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。