- public class GetBroadcastextends BroadcastReceiver {
- private static GetBroadcast mReceiver =new GetBroadcast();
- private static IntentFilter mIntentFilter;
- public staticvoid registerReceiver(Context context) {
- mIntentFilter = new IntentFilter();
- mIntentFilter.addDataScheme("package");
- mIntentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
- mIntentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
- mIntentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
- context.registerReceiver(mReceiver, mIntentFilter);
- }
- public staticvoid unregisterReceiver(Context context) {
- context.unregisterReceiver(mReceiver);
- }
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
- Toast.makeText(context, "有应用被添加", Toast.LENGTH_LONG).show();
- } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
- Toast.makeText(context, "有应用被删除", Toast.LENGTH_LONG).show();
- }
- /*
- * else if(Intent.ACTION_PACKAGE_CHANGED.equals(action)){
- * Toast.makeText(context, "有应用被改变", Toast.LENGTH_LONG).show(); }
- */
- else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
- Toast.makeText(context, "有应用被替换", Toast.LENGTH_LONG).show();
- }
- /*
- * else if(Intent.ACTION_PACKAGE_RESTARTED.equals(action)){
- * Toast.makeText(context, "有应用被重启", Toast.LENGTH_LONG).show(); }
- */
- /*
- * else if(Intent.ACTION_PACKAGE_INSTALL.equals(action)){
- * Toast.makeText(context, "有应用被安装", Toast.LENGTH_LONG).show(); }
- */
- }
- }
public class GetBroadcast extends BroadcastReceiver {
private static GetBroadcast mReceiver = new GetBroadcast();
private static IntentFilter mIntentFilter;
public static void registerReceiver(Context context) {
mIntentFilter = new IntentFilter();
mIntentFilter.addDataScheme("package");
mIntentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
mIntentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
mIntentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
context.registerReceiver(mReceiver, mIntentFilter);
}
public static void unregisterReceiver(Context context) {
context.unregisterReceiver(mReceiver);
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
Toast.makeText(context, "有应用被添加", Toast.LENGTH_LONG).show();
} else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
Toast.makeText(context, "有应用被删除", Toast.LENGTH_LONG).show();
}
/*
* else if(Intent.ACTION_PACKAGE_CHANGED.equals(action)){
* Toast.makeText(context, "有应用被改变", Toast.LENGTH_LONG).show(); }
*/
else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
Toast.makeText(context, "有应用被替换", Toast.LENGTH_LONG).show();
}
/*
* else if(Intent.ACTION_PACKAGE_RESTARTED.equals(action)){
* Toast.makeText(context, "有应用被重启", Toast.LENGTH_LONG).show(); }
*/
/*
* else if(Intent.ACTION_PACKAGE_INSTALL.equals(action)){
* Toast.makeText(context, "有应用被安装", Toast.LENGTH_LONG).show(); }
*/
}
}
public class BootReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent){
//接收安装广播
if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
String packageName = intent.getDataString();
System.out.println("安装了:" +packageName + "包名的程序");
}
//接收卸载广播
if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {
String packageName = intent.getDataString();
System.out.println("卸载了:" + packageName + "包名的程序");
}
}
}
第一、 新建监听类:BootReceiver继承BroadcastReceiver#
- public class BootReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- //接收广播:系统启动完成后运行程序
- if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
- Intent newIntent = new Intent(context, WatchInstall.class);
- newIntent.setAction("android.intent.action.MAIN");
- newIntent.addCategory("android.intent.category.LAUNCHER");
- newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- context.startActivity(newIntent);
- }
- //接收广播:设备上新安装了一个应用程序包后自动启动新安装应用程序。
- if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
- String packageName = intent.getDataString().substring(8);
- System.out.println("---------------" + packageName);
- Intent newIntent = new Intent();
- newIntent.setClassName(packageName,packageName+ .MainActivity");
- newIntent.setAction("android.intent.action.MAIN");
- newIntent.addCategory("android.intent.category.LAUNCHER");
- newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- context.startActivity(newIntent);
- }
- //接收广播:设备上删除了一个应用程序包。
- if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {
- System.out.println("********************************");
- DatabaseHelper dbhelper = new DatabaseHelper();
- dbhelper.executeSql("delete from users");
- }
- }
第二、 修改AndroidManifest.xml配置文件#
- <?xml version="1.0" encoding="UTF-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="org.me.watchinstall">
- <application>
- <receiver android:name=".BootReceiver"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED"/>
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- <intent-filter>
- <action android:name="android.intent.action.PACKAGE_ADDED" />
- <action android:name="android.intent.action.PACKAGE_REMOVED" />
- <data android:scheme="package" />
- <!-- 注意!! 这句必须要加,否则接收不到BroadCast -->
- </intent-filter>
- </receiver>
- <activity android:name=".WatchInstall" android:label="WatchInstall">
- <intent-filter>
- <action android:name="android.intent.action.MAIN"/>
- <category android:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- </application>
- <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>