android 监听 APK 安装 与 删除等过程
这是我项目里需要在安装完应用后,马上能侦听到新的应用安装成功,并且更新相应的界面用到的
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()
@Overrideprotected void onDestroy()
{
super.onDestroy();
//unregisterReceiver(apkInstallListener);
}
更多相关文章
- 【边做项目边学Android】手机安全卫士05_2:程序主界面,为每个条目
- 2013年 Android开发环境安装
- react-native 中配置极光推送 Android关键详细的说明与截图代码
- React Native for Android(安卓)实战(一):配置和起步
- Android(安卓)学习记录-零散知识点
- Android(安卓)framework自定义待机广播(可静态注册)
- Android(安卓)Studio2.3正式版带SDK安装教程 亲测有效!
- 关于Mac升级Android(安卓)Studio无法获取安装目录权限的解决办法
- android Window和ViewRootImpl