Android編程監(jiān)聽APK安裝與刪除等過程的方法
本文實例講述了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, "有應用被添加", Toast.LENGTH_LONG).show();
}
else if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){
Toast.makeText(context, "有應用被刪除", Toast.LENGTH_LONG).show();
}
/* else if(Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())){
Toast.makeText(context, "有應用被改變", Toast.LENGTH_LONG).show();
}*/
else if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){
Toast.makeText(context, "有應用被替換", Toast.LENGTH_LONG).show();
}
/* else if(Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())){
Toast.makeText(context, "有應用被重啟", Toast.LENGTH_LONG).show();
}*/
/* else if(Intent.ACTION_PACKAGE_INSTALL.equals(intent.getAction())){
Toast.makeText(context, "有應用被安裝", 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("******應用添加***"+isDeleted);
Toast.makeText(context, "有應用被添加"+isDeleted, Toast.LENGTH_LONG).show();
}
else if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){
System.out.println("*****應用被刪除");
Toast.makeText(context, "有應用被刪除", Toast.LENGTH_LONG).show();
}
/* else if(Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())){
Toast.makeText(context, "有應用被改變", Toast.LENGTH_LONG).show();
}*/
else if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){
System.out.println("****應用被替換");
Toast.makeText(context, "有應用被替換", Toast.LENGTH_LONG).show();
}
/* else if(Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())){
Toast.makeText(context, "有應用被重啟", Toast.LENGTH_LONG).show();
}*/
/* else if(Intent.ACTION_PACKAGE_INSTALL.equals(intent.getAction())){
Toast.makeText(context, "有應用被安裝", 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相關內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設計有所幫助。
相關文章
正確在Flutter中添加webview實現(xiàn)詳解
這篇文章主要為大家介紹了正確在Flutter中添加webview實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
Android中獲取資源 id 及資源 id 的動態(tài)獲取
這篇文章主要介紹了 Android中獲取資源 id 及資源 id 的動態(tài)獲取的相關資料,需要的朋友可以參考下2017-01-01
View Controller Transition實現(xiàn)京東加購物車效果
這篇文章主要介紹了View Controller Transition實現(xiàn)京東加購物車效果,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-02-02
Android開發(fā)實現(xiàn)的簡單計算器功能【附完整demo源碼下載】
這篇文章主要介紹了Android開發(fā)實現(xiàn)的簡單計算器功能,結(jié)合實例形式分析了Android計算器的具體實現(xiàn)步驟與相關操作技巧,并附帶完整demo源碼供讀者下載參考,需要的朋友可以參考下2017-11-11

