Android开发片段–监控程序安装,修改,删除状态
java
package com.zzh.PackageReceiver;import android.content.BroadcastReceiver;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.util.Log;import android.widget.Toast;public class PackageReceiver extends BroadcastReceiver {private String TAG = "PACER";private static String PACERSTATUS = "package:com.zzh.pacerstatus";@Overridepublic void onReceive(Context context, Intent intent) {Log.d(TAG, "Receive SIP Action Responses is : " + intent.getAction());try {Log.d(TAG,"Receive SIP Action Responses is : " + intent.getAction());if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) {//Toast.makeText(context, "有应用被添加" + intent.getDataString(), Toast.LENGTH_LONG).show();Log.i("TAG", "有应用被添加" + intent.getDataString());if(PACERSTATUS.equals(intent.getDataString())) {StartPacerStatus(context);}} else if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {//Toast.makeText(context, "有应用被删除" + intent.getDataString(), Toast.LENGTH_LONG).show();Log.i("TAG", "有应用被删除" + intent.getDataString());}/* * 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, "有应用被替换" + intent.getPackage(), Toast.LENGTH_LONG).show();Log.i("TAG", "有应用被替换" + intent.getPackage());}/* * 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(); } */} catch (Exception e) {Log.d(TAG, "PhoneDaemonDemo UI application no start.");}}public void StartPacerStatus(Context context) {Log.i(TAG, "Service Start1");Intent serviceIntent = new Intent(Intent.ACTION_MAIN);serviceIntent.addCategory(Intent.CATEGORY_DEFAULT);ComponentName cn = new ComponentName("com.zzh.pacerstatus","com.zzh.pacerstatus.initVersionService");serviceIntent.setComponent(cn);context.startService(serviceIntent);Intent serviceIntent1 = new Intent(Intent.ACTION_MAIN);ComponentName cn1 = new ComponentName("com.zzh.pacerstatus","com.zzh.pacerstatus.VersionService");serviceIntent1.setComponent(cn1);context.startService(serviceIntent1);Log.i(TAG, "Service Start");}}AndroidManifest.xml
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <receiver android:name=".PackageReceiver" > <intent-filter > <action android:name="android.intent.action.PACKAGE_ADDED" /> <action android:name="android.intent.action.PACKAGE_REMOVED" /> <action android:name="android.intent.action.PACKAGE_REPLACED" /> <data android:scheme="package" /> </intent-filter> </receiver> </application>?