android 侦听apk安装成功,再执行界面的更新操作
来源:互联网 发布:跑步软件排行 编辑:程序博客网 时间:2024/05/21 21:45
这是我项目里需要在安装完应用后,马上能侦听到新的应用安装成功,并且更新相应的界面用到的
1.项目里添加侦听类,然后配置文件加权限,就ok
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>
2.代码实现添加
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()
@Overrideprotected void onDestroy()
{
super.onDestroy();
//unregisterReceiver(apkInstallListener);
}
- android 侦听apk安装成功,再执行界面的更新操作
- 调用android系统内部的安装apk应用界面进行安装应用程序,例如需要更新应用程序
- android更新安装apk
- Android做检测版本更新安装完以后就什么都没有了,没有出现安装成功的界面
- Android APK 安装/更新问题
- Android 设置安装后界面显示的apk名
- Android下载完成更新后,闪退,并没有打开安装成功界面
- Android下载完成更新后,没有打开安装成功界面,出现闪退问题
- android静默安装apk已经成功实现
- android静默安装apk已经成功实现
- android静默安装apk已经成功实现
- android静默安装apk已经成功实现
- Android 更新下载apk 安装apk 打开apk
- 关于 android apk安装 更新 卸载的几个问题
- APK的下载更新安装
- 普通的APK安装更新
- 代码中执行apk安装、卸载、更新以及静默安装
- 代码中执行apk安装、卸载、更新以及静默安装
- 内核怎样管理你的内存
- Android中全局搜索(QuickSearchBox)详解
- Spring学习之中文乱码问题
- NDK开发Javah生成Jni 头文件
- 中国著名绢本画家金士焯
- android 侦听apk安装成功,再执行界面的更新操作
- shell中对单引号的处理问题
- Spring Bean 高级装配
- Windows Socket I/O模型以及Linux Epoll模型的有关资料
- 识别不了移动硬盘的解决方法
- android XML下searchable.xml
- Linux下phpMyAdmin 的配置
- 怎么判断 TCP链接 是否断开了呢
- 判断当前环境是中文还是英文