Android编程监听APK安装与删除等过程的方法
程序员文章站
2023-12-11 15:14:46
本文实例讲述了android编程监听apk安装与删除等过程的方法。分享给大家供大家参考,具体如下:
软件下载后的一系列动作监听:先前是通过service监听扫描获取状态,...
本文实例讲述了android编程监听apk安装与删除等过程的方法。分享给大家供大家参考,具体如下:
软件下载后的一系列动作监听:先前是通过service监听扫描获取状态,以后用这个方法测试使用
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>
代码实现添加:
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(); }*/ } }; // 注册监听 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里的调用 registersdcardlistener()
@override protected void ondestroy() { super.ondestroy(); //unregisterreceiver(apkinstalllistener); }
更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。