方法一:
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_REPLACED.equals(intent.getAction())){
Toast.makeText(context, "有应用被替换", Toast.LENGTH_LONG).show();
}
else if(Intent.ACTION_CAMERA_BUTTON.equals(intent.getAction())){
Toast.makeText(context, "按键", Toast.LENGTH_LONG).show();
}
}
}
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_REPLACED.equals(intent.getAction())){
Toast.makeText(context, "有应用被替换", Toast.LENGTH_LONG).show();
}
else if(Intent.ACTION_CAMERA_BUTTON.equals(intent.getAction())){
Toast.makeText(context, "按键", Toast.LENGTH_LONG).show();
}
}
}
需要声明的权限如下 Android Manifest.xml
<?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>-->
<action android:name="android.intent.action.CAMERA_BUTTON"></action>
<data android:scheme="package"></data>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
<?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>-->
<action android:name="android.intent.action.CAMERA_BUTTON"></action>
<data android:scheme="package"></data>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
方法二:
通过阅读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
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 应用程序的安装和删除
详细的实现代码如下
getBroadcast.java
package zy.Broadcast;
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();
}*/
}
}
package zy.Broadcast;
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>即可在系统里捕获这些广
播消息
具体的源代码如下
view plaincopy to clipboardprint?
<?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>
<?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>
把程序安装之后,系统就会注册这个BroadcastReceiver
然后有应用安装删除替换操作时时,就会弹出Toast 提示

更多相关文章

  1. Android读取系统时间
  2. 系统启动之一
  3. android 系统广播
  4. 常用的系统调用【Android】
  5. 深入显出 - Android系统移植与平台开发(一)

随机推荐

  1. Android中shape的使用
  2. Android帮助文档翻译——开发指南(十五)获
  3. Android预定义样式?android:attr/attribu
  4. android之CalendarView日历视图
  5. Android之隐式意图(Intent)如何查找匹配
  6. Android开发之如何获取Android手机屏幕的
  7. 【android开发】解析xml文件①
  8. Android(安卓)imageview图片缩放实现
  9. Unity和Android交互(持续更新)
  10. Android(安卓)Layout的layout_height等属