Android监听程序的安装和卸载
2015-07-01 17:11
393 查看
在android系统中,安装和卸载都会发送广播,当应用安装完成后系统会发android.intent.action.PACKAGE_ADDED广播。可以通过intent.getDataString()获得所安装的包名。当卸载程序时系统发android.intent.action.PACKAGE_REMOVED广播。同样intent.getDataString()获得所卸载的包名。应用程序无法监听自己的安装与卸载,但覆盖安装可以监听到自己的android.intent.action.PACKAGE_REMOVED广播。public class PackageReceiver extends BroadcastReceiver{@Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) { String packageName = intent.getDataString(); Log.i("Test","---------------" + packageName); } if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) { String packageName = intent.getDataString(); Log.i("Test","---------------" + "PACKAGE_REMOVED" + packageName); }}}复制代码<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="测试"> <receiver android:name=".PackageReceiver" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <action android:name="android.intent.action.PACKAGE_REMOVED" /> <data android:scheme="package" /> </intent-filter> </receiver> </application> <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RESTART_PACKAGES"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/></manifest>这里是手动注册广播
public class GetBroadcast extends BroadcastReceiver { |
02 |
03 | private static GetBroadcast mReceiver = new GetBroadcast(); |
04 | private static IntentFilter mIntentFilter; |
05 |
06 |
07 | public static void registerReceiver(Context context) { |
08 | mIntentFilter = new IntentFilter(); |
09 | mIntentFilter.addDataScheme( "package" ); |
10 | mIntentFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
11 | mIntentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
12 | mIntentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED); |
13 | context.registerReceiver(mReceiver, mIntentFilter); |
14 | } |
15 |
16 | public static void unregisterReceiver(Context context) { |
17 | context.unregisterReceiver(mReceiver); |
18 | } |
19 |
20 | @Override |
21 | public void onReceive(Context context, Intent intent) { |
22 | String action = intent.getAction(); |
23 | if (Intent.ACTION_PACKAGE_ADDED.equals(action)) { |
24 | Toast.makeText(context, "有应用被添加" , Toast.LENGTH_LONG).show(); |
25 | } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) { |
26 | Toast.makeText(context, "有应用被删除" , Toast.LENGTH_LONG).show(); |
27 | } |
28 |
29 | /* |
30 | * else if(Intent.ACTION_PACKAGE_CHANGED.equals(action)){ |
31 | * |
32 | */ |
33 |
34 | else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) { |
35 | Toast.makeText(context, "有应用被替换", Toast.LENGTH_LONG).show(); |
36 | } |
37 |
38 | /* |
39 | * else if(Intent.ACTION_PACKAGE_RESTARTED.equals(action)){ |
40 | * |
41 | */ |
42 |
43 | /* |
44 | * else if(Intent.ACTION_PACKAGE_INSTALL.equals(action)){ |
45 | * |
46 | */ |
47 | } |
48 |
} |
相关文章推荐
- Android 在一个Activity的一部分中显示其他Activity
- Android编译相关
- Android Studio 右侧条标高亮显示选中的相同变量,类,方法。。。
- android 实现2张图片层叠效果
- Android 图片在SD卡及包下的存储
- Android中间向两端,两端向中间并发前进的进度条,Android自定义ProgressBar进度条
- Android 用Animation-list实现逐帧动画
- 获取SD卡下Android/data/下文件
- android style.xml的使用
- Android] Android XML解析学习——方式比较
- 【Android】【Framework】AndroidFramework之ContextImpl
- 【Android】【Framework】AndroidFramework之ContextImpl
- 【Android】【Framework】AndroidFramework之ContextImpl
- 【Android】【Framework】AndroidFramework之ContextImpl
- 【Android】【Framework】AndroidFramework之ContextImpl
- 【Android】【Framework】AndroidFramework之ContextImpl
- 【Android】【Framework】AndroidFramework之ContextImpl
- Android平台中实现对XML的三种解析方式
- Android listView 之万能Adapter
- android studio Process 'command