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()

@Override
protected void onDestroy()
{
super.onDestroy();
//unregisterReceiver(apkInstallListener);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值