您的当前位置:首页正文

android 监听 APK 安装 与 删除等过程

2024-11-30 来源:个人技术集锦

这是我项目里需要在安装完应用后,马上能侦听到新的应用安装成功,并且更新相应的界面用到的

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=""
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);
}
显示全文