<receiver android:name="com.haolianluo.net.session.module.HAPKInstallReceiver">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.PACKAGE_ADDED"></action>
<data android:scheme="package"/>
</intent-filter>

<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.PACKAGE_REMOVED"></action>
<data android:scheme="package"/>
</intent-filter>
</receiver>
通过阅读Android SDK里关于intent.action这部分里面的描述,我们可以找到一些与package相关的系统广播
android.intent.action.PACKAGE_ADDED
android.intent.action.PACKAGE_CHANGED
android.intent.action.PACKAGE_DATA_CLEARED
android.intent.action.PACKAGE_INSTALL
android.intent.action.PACKAGE_REMOVED
android.intent.action.PACKAGE_REPLACED
android.intent.action.PACKAGE_RESTARTED
其中

ACTION_PACKAGE_ADDED

在SDK里的描述是

Broadcast Action: A new application package has been installed on the device.

ACTION_PACKAGE_REMOVED

在SDK里的描述是

Broadcast Action: An existing application package has been removed from the device.

ACTION_PACKAGE_REPLACED

在SDK里的描述是

Broadcast Action: A new version of an application package has been installed, replacing an existing version that was previously installed.



通过这三个广播消息 我们已经可以监控到Android 应用程序的安装和删除



详细的实现代码如下

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class getBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){
Toast.makeText(context, "有应用被添加", Toast.LENGTH_LONG).show();
}
else if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){
Toast.makeText(context, "有应用被删除", Toast.LENGTH_LONG).show();
}
/* else if(Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())){
Toast.makeText(context, "有应用被改变", Toast.LENGTH_LONG).show();
}*/
else if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){
Toast.makeText(context, "有应用被替换", Toast.LENGTH_LONG).show();
}
/* else if(Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())){
Toast.makeText(context, "有应用被重启", Toast.LENGTH_LONG).show();
}*/
/* else if(Intent.ACTION_PACKAGE_INSTALL.equals(intent.getAction())){
Toast.makeText(context, "有应用被安装", Toast.LENGTH_LONG).show();
}*/

}

}
然后在AndroidManifest.xml中声明这几个Action的<intent-filter>即可在系统里捕获这些广播消息

具体的源代码如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="zy.Broadcast"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Broadcast"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="getBroadcast" android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"></action>
<!-- <action android:name="android.intent.action.PACKAGE_CHANGED"></action>-->
<action android:name="android.intent.action.PACKAGE_REMOVED"></action>
<action android:name="android.intent.action.PACKAGE_REPLACED"></action>
<!-- <action android:name="android.intent.action.PACKAGE_RESTARTED"></action>-->
<!-- <action android:name="android.intent.action.PACKAGE_INSTALL"></action>-->
<data android:scheme="package"></data>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />

</manifest>

更多相关文章

  1. 2011.09.01(4)——— android 应用程序跳转到桌面
  2. Android(安卓)process与Thread 的问题
  3. Android官方入门文档[11]支持不同平台版本
  4. android文件操作的实例
  5. System Permissions—— android系统权限
  6. Android(安卓)各种机型兼容问题
  7. 高通 rom 分区表
  8. iOS程序猿的Android之路--Android的前世今生
  9. Android(安卓)Auto

随机推荐

  1. sdcard无法挂载问题
  2. Android文档阅读02—解析Android程序
  3. Android编程 获取网络连接状态 及调用网
  4. Android 使用类加载器原理实现热修复
  5. android登录Web以及登录保持
  6. Android提供的系统服务之--LayoutInflate
  7. android使用高德地图api
  8. cocos2dx--cocos2dx3.1.1android环境编译
  9. Android高手进阶教程(九)----Android Han
  10. Android(安卓)TabLayout 不显示标题的解