public class PushReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){ System.out.println("手机开机了...bootComplete!"); }else if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){ System.out.println("新安装了应用程序....pakageAdded!"); }else if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){ System.out.println("应用程序被卸载了....pakageRemoved!"); }else if(Intent.ACTION_USER_PRESENT.equals(intent.getAction())){ System.out.println("手机被唤醒了.....userPresent"); Intent service = new Intent(); service.setAction("com.xxx.service.PushService"); service.setClass(context, PushService.class); context.startService(service); } } }
Mainfest中注册receiver:
<!-- push receiver --> <receiver android:name=".receiver.PushReceiver"> <intent-filter> <!-- 手机开机 --> <action android:name="android.intent.action.BOOT_COMPLETED"></action> <!-- 手机唤醒解锁 --> <action android:name="android.intent.action.USER_PRESENT" /> </intent-filter> <intent-filter> <!-- 程序包安装与卸载 --> <action android:name="android.intent.action.PACKAGE_ADDED"></action> <action android:name="android.intent.action.PACKAGE_REMOVED"></action> <data android:scheme="package"></data> </intent-filter> </receiver>