标签:
[摘要:题目: 正在运用开辟中碰到须要如许的需供:正在用户下载我们的运用装置以后删除装置包。 办理: android会正在每一个中界操纵APK的举措以后收回体系级其余播送,过滤器称号:]问题:
在应用开发中遇到需要这样的需求:在用户下载我们的应用安装之后删除安装包。
解决:
android会在每个外界操作APK的动作之后发出系统级别的广播,过滤器名称: android.intent.action.package_ADDED,
android.intent.action.PACKAGE_REMOVED,
需要注意点的是在涉及到存储卡状态改变的时候必须添加
<data android:scheme="package" >
我们要做的就是在应用AndroidManifest中注册静态广播,并在自定义的广播里处理相应的逻辑:
<receiver
android:name="com.example.testdeleteapk.receiver.InitApkBroadCastReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
这样,在应用安装成功后就会接受到相应的广播。广播定义如下:
package com.example.testdeleteapk.receiver;
import Java.io.File;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
public class InitApkBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) {
system.out.println("监听到系统广播添加");
}
if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
System.out.println("监听到系统广播移除");
}
if (Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())) {
System.out.println("监听到系统广播替换");
}
}
}
标签:
原文地址:http://blog.csdn.net/l970859633/article/details/51364161