在xml中注册
<!-- 开机广播 --> <receiver android:name=".receiver.BootBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </receiver>
启动 app核心服务
package com.boai.base.receiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import com.boai.base.service.CoreService; public class BootBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // 启动App核心Service Intent startCoreServiceIntent = new Intent(context, CoreService.class); context.startService(startCoreServiceIntent); } }
服务类
public class CoreService extends Service
在服务类中 注册广播
@Override public void onCreate() { super.onCreate(); // 注册广播 regBroadcastReceiver(); }
生成需要的广播
private void regBroadcastReceiver() { IntentFilter mPushMsgBrFileter = new IntentFilter(); mPushMsgBrFileter.addAction(Constants.BR_ACTION_UMENG_PUSH_MSG); mPushMsgBrFileter.addAction(Constants.BR_ACTION_USER_LOGIN); mPushMsgBrFileter.addAction(Constants.BR_ACTION_USER_LOGOUT); registerReceiver(mBroadcastReceiver, mPushMsgBrFileter); }
广播接收处理
//创建一个可根据需要创建新线程的线程池
mThreadPool = Executors.newCachedThreadPool();
// 广播接收 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (Constants.BR_ACTION_UMENG_PUSH_MSG.equals(action)) { // 推送消息 if (AppCookie.getUserId() == -1) { return ; } final Serializable tempObj = intent.getSerializableExtra(Constants.OBJECT); if (null == tempObj) { return ; } mThreadPool.submit(new Runnable() { @Override public void run() { handlerPushMsg((PushMsgBean)tempObj); } }); } else if (Constants.BR_ACTION_USER_LOGIN.equals(action)) { // 登录 // 更新用户位置 mHandler.removeMessages(HANDLER_WHAT_UPDATE_USER_LOCALTION); mHandler.sendEmptyMessage(HANDLER_WHAT_UPDATE_USER_LOCALTION); } else if (Constants.BR_ACTION_USER_LOGOUT.equals(action)) { // 登出,移除相应广播 mHandler.removeMessages(HANDLER_WHAT_UPDATE_USER_LOCALTION); } } };
/**
* 处理推送消息
* @param retPushMsg 推送消息对象
*/
private void handlerPushMsg(PushMsgBean retPushMsg) { //... // 广播给需要通知刷新的界面 msg.setReadStatus(0); Intent intent = new Intent(Constants.BR_ACTION_NEW_MSG); intent.putExtra(Constants.OBJECT, msg); sendBroadcast(intent); // 是否免打扰 boolean isNofityNoDisturb = AppCookie.getBoolean(Constants.STATUS_BUSINESS_PUSH, false); if (!isNofityNoDisturb) { // 发送显示通知栏 mHandler.obtainMessage(HANDLER_WHAT_NEW_MSG_PUSH, msgDbID, 0, retPushMsg).sendToTarget(); } }
//handler
// Handler处理 private static class MyHandler extends Handler { private WeakReference<CoreService> wrf; public MyHandler(CoreService coreService) { wrf = new WeakReference<>(coreService); } @Override public void handleMessage(Message msg) { super.handleMessage(msg); final CoreService curCoreService = wrf.get(); if (null == curCoreService) { return ; } switch (msg.what) { case HANDLER_WHAT_NEW_MSG_PUSH: { final PushMsgBean curPushMsg = (PushMsgBean)msg.obj; final int msgDbId = msg.arg1; int msgType = curPushMsg.getMsgtype(); if (msgType == MsgTypeEnum.FOLLOW.getTypeValue()) { try { String url = AppUtil.getUnifyImageUrl(ImageType.USER_ICON, Long.parseLong(curPushMsg.getSid())); ImageLoader.getInstance().loadImage(url, new ImageLoadingListener() { @Override public void onLoadingStarted(String s, View view) { } @Override public void onLoadingFailed(String s, View view, FailReason failReason) { curCoreService.mNotificationUtil.showPushMsgNotify(curPushMsg, msgDbId, null); } @Override public void onLoadingComplete(String s, View view, Bitmap bitmap) { curCoreService.mNotificationUtil.showPushMsgNotify(curPushMsg, msgDbId, bitmap); } @Override public void onLoadingCancelled(String s, View view) { curCoreService.mNotificationUtil.showPushMsgNotify(curPushMsg, msgDbId, null); } }); } catch (Exception e) { e.printStackTrace(); curCoreService.mNotificationUtil.showPushMsgNotify(curPushMsg, msgDbId, null); } } else { curCoreService.mNotificationUtil.showPushMsgNotify(curPushMsg, msgDbId, null); } break; } } } }
服务关闭
@Override public void onDestroy() { super.onDestroy(); unregisterReceiver(mBroadcastReceiver); }