在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
{ |
03 | private static GetBroadcast
mReceiver = new GetBroadcast(); |
04 | private static IntentFilter
mIntentFilter; |
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); |
16 | public static void unregisterReceiver(Context
context) { |
17 | context.unregisterReceiver(mReceiver); |
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(); |
30 | *
else if(Intent.ACTION_PACKAGE_CHANGED.equals(action)){ |
31 | * Toast.makeText(context,"有应用被改变", Toast.LENGTH_LONG).show(); } |
34 | else
if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) { |
35 | Toast.makeText(context,
"有应用被替换", Toast.LENGTH_LONG).show(); |
39 | *
else if(Intent.ACTION_PACKAGE_RESTARTED.equals(action)){ |
40 | * Toast.makeText(context,"有应用被重启", Toast.LENGTH_LONG).show(); } |
44 | *
else if(Intent.ACTION_PACKAGE_INSTALL.equals(action)){ |
45 | * Toast.makeText(context,"有应用被安装", Toast.LENGTH_LONG).show(); } |
| }from:http://blog.csdn.net/tianxiangshan/article/details/7921422 |