创建一个java 类 MyInstalledReceiver,继承broadcastReceiver
package com.meefo.fengweihezi_androID;import androID.content.broadcastReceiver;import androID.content.Context;import androID.content.Intent;public class MyInstalledReceiver extends broadcastReceiver { @OverrIDe public voID onReceive(Context context, Intent intent){ //接收安装广播 if (intent.getAction().equals("androID.intent.action.PACKAGE_ADDED")) { String packagename = intent.getDataString(); System.out.println("安装了:" +packagename + "包名的程序"); } //接收卸载广播 if (intent.getAction().equals("androID.intent.action.PACKAGE_REMOVED")) { String packagename = intent.getDataString(); System.out.println("卸载了:" + packagename + "包名的程序"); } }}
到AndroIDManifest xml文件里注册广播地址
<receiver androID:name=".MyInstalledReceiver" > <intent-filter> <action androID:name="androID.intent.action.PACKAGE_ADDED" /> <action androID:name="androID.intent.action.PACKAGE_REMOVED" /> <data androID:scheme="package" /> </intent-filter> </receiver>
Activity里重写MyInstalledReceiver的onReceiver方法
private final MyInstalledReceiver installedReceiver = new MyInstalledReceiver(){ @OverrIDe public voID onReceive(Context context, Intent intent){ if (intent.getAction().equals("androID.intent.action.PACKAGE_ADDED")) {// String packagename = intent.getDataString(); //这里就可以写你想写的动作了。这里相当于是一个软件安装完成的回调 } } };
定义一个初始化方法,然后在程序框架初始化的位置调用这个方法
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(installedReceiver, intentFilter); }
总结 以上是内存溢出为你收集整理的监听安卓apk的安装再采取相应动作全部内容,希望文章能够帮你解决监听安卓apk的安装再采取相应动作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)