这是我项目里需要在安装完应用后,马上能侦听到新的应用安装成功,并且更新相应的界面用到的
1.项目里添加侦听类,然后配置文件加权限,就ok
importandroid.content.BroadcastReceiver;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.widget.Toast;
publicclassgetBroadcastextendsBroadcastReceiver{
@Override
publicvoidonReceive(Contextcontext,Intentintent){
if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){
Toast.makeText(context,"有应用被添加",Toast.LENGTH_LONG).show();
}
elseif(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){
Toast.makeText(context,"有应用被删除",Toast.LENGTH_LONG).show();
}
/*elseif(Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())){
Toast.makeText(context,"有应用被改变",Toast.LENGTH_LONG).show();
}*/
elseif(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){
Toast.makeText(context,"有应用被替换",Toast.LENGTH_LONG).show();
}
/*elseif(Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())){
Toast.makeText(context,"有应用被重启",Toast.LENGTH_LONG).show();
}*/
/*elseif(Intent.ACTION_PACKAGE_INSTALL.equals(intent.getAction())){
Toast.makeText(context,"有应用被安装",Toast.LENGTH_LONG).show();
}*/
}
}
<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="zy.Broadcast"
android:versionCode="1"
android:versionName="1.0">
<applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
<activityandroid:name=".Broadcast"
android:label="@string/app_name">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiverandroid:name="getBroadcast"android:enabled="true">
<intent-filter>
<actionandroid:name="android.intent.action.PACKAGE_ADDED"></action>
<!--<actionandroid:name="android.intent.action.PACKAGE_CHANGED"></action>-->
<actionandroid:name="android.intent.action.PACKAGE_REMOVED"></action>
<actionandroid:name="android.intent.action.PACKAGE_REPLACED"></action>
<!--<actionandroid:name="android.intent.action.PACKAGE_RESTARTED"></action>-->
<!--<actionandroid:name="android.intent.action.PACKAGE_INSTALL"></action>-->
<dataandroid:scheme="package"></data>
</intent-filter>
</receiver>
</application>
<uses-sdkandroid:minSdkVersion="7"/>
</manifest>
2.代码实现添加
privatefinalBroadcastReceiverapkInstallListener=newBroadcastReceiver(){
@Override
publicvoidonReceive(Contextcontext,Intentintent){
if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){
System.out.println("**************Broadcase*************");
Filefile=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();
}
elseif(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){
System.out.println("*****应用被删除");
Toast.makeText(context,"有应用被删除",Toast.LENGTH_LONG).show();
}
/*elseif(Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())){
Toast.makeText(context,"有应用被改变",Toast.LENGTH_LONG).show();
}*/
elseif(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){
System.out.println("****应用被替换");
Toast.makeText(context,"有应用被替换",Toast.LENGTH_LONG).show();
}
/*elseif(Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())){
Toast.makeText(context,"有应用被重启",Toast.LENGTH_LONG).show();
}*/
/*elseif(Intent.ACTION_PACKAGE_INSTALL.equals(intent.getAction())){
Toast.makeText(context,"有应用被安装",Toast.LENGTH_LONG).show();
}*/
}
};
//注册监听
privatevoidregisterSDCardListener(){
IntentFilterintentFilter=newIntentFilter(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编程监听APK安装与删除等过程的方法,涉及Android事件监听、权限控制、广播操作等相关实现技巧,需要的朋友可以参考下
App自监听卸载 Android监听程序自身被卸载 ccccccccccccccccccccccc
RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android xmlns:tools=http://schemas.android.com/tools android:layout_width=match_parent android:layout_height=match_parent tools:context=...
第1章 androi简介 ...8.11 远程下载安装android程序 8.11.1 apk简介 8.11.2 下载apk程序 8.11.3 安装apk程序 8.11.4 移除apk程序 8.11.5 本实例原理 8.11.6 具体实现 8.12 下载并观看视频 8.13 小结
8.2.2 Window的删除过程 301 8.2.3 Window的更新过程 303 8.3 Window的创建过程 304 8.3.1 Activity的Window创建过程 304 8.3.2 Dialog的Window创建过程 308 8.3.3 Toast的Window创建过程 311 第9章 四大组件...
8.2.2 Window的删除过程 / 301 8.2.3 Window的更新过程 / 303 8.3 Window的创建过程 / 304 8.3.1 Activity的Window创建过程 / 304 8.3.2 Dialog的Window创建过程 / 308 8.3.3 Toast的Window创建过程 / ...
/ 288 7.4 使用动画的注意事项 / 292 第8章 理解Window和Window Manager / 294 8.1 Window和Window Manager / 294 8.2 Window的内部机制 / 297 8.2.1 Window的添加过程 / 298 8.2.2 Window的删除过程 ...
可以完成添加、删除、选择操作的标签控件,可以自定样式(颜色等),可删除的标签末尾带有'×'符号。项目地址:https://github.com/namito/TagCloudLinkView效果图:如何使用: android:id="@ id/...
第1章 Android计算平台简介 1.1 面向新PC的全新平台 1.2 Android的历史 1.3 Dalvik VM剖析 1.4 理解Android软件栈 1.5 使用Android SDK开发最终用户应用程序 1.5.1 Android模拟器 1.5.2 ...
1、ANDROID文件系统与应用程序架构 7 1.1、ANDROID 文件系统 7 1.2、ANDROID应用程序架构 9 2、ANDROID应用程序结构 11 2.1、ACTIVITY 12 2.1.1、概述 12 2.1.2、Activity的生命周期 15 2.1.3、Activity 的创建 16 ...
1、ANDROID文件系统与应用程序架构 7 1.1、ANDROID 文件系统 7 1.2、ANDROID应用程序架构 9 2、ANDROID应用程序结构 11 2.1、ACTIVITY 12 2.1.1、概述 12 2.1.2、Activity的生命周期 15 2.1.3、Activity 的创建 16 ...
Android手机监听短信并根据指定规则转发到其他手机,钉钉机器人,企业微信群机器人,邮箱,树皮,webhook等 :warning:此项目在的基础上优化改造而来,感谢原作者! 特别声明: 本仓库发布的SmsForwarder项目中涉及...
|--Intent启动应用apk安装 |--Intent常用功能 |--IO将输入流转成字节 |--Json读js资源文件 |--layout布局样式之style配置 |--listview 页面 图片加文字 |--ListView之CursorAdapter异步查询框架之短信 |--ListView...
ADB,即 Android Debug Bridge,它是 Android 开发/测试人员不可替代的强大工具,也是 Android 设备玩家的好玩具。...INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES 已安装该应用,且签名与 APK 文件不一致 先...
第1章 Android计算平台简介 1.1 面向新PC的全新平台 1.2 Android的历史 1.3 Dalvik VM剖析 1.4 理解Android软件栈 1.5 使用Android SDK开发最终用户应用程序 1.5.1 Android模拟器 1.5.2 ...
tinyCam Monitor最好的Android应用程序,用于远程监视,控制和视频记录您的专用/公用网络或IP摄像机,带有CCTV摄像机的IP编码器和DVR,下载次数超过5百万次。远程安全地监视您的婴儿,宠物,家庭,企业,交通和天气...