在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);
}

  


最新文章

  1. VS2012 Unit Test —— 我对IdleTest库动的大手术以及对Xml相关操作进行测试的方式
  2. Thinkphp内置截取字符串函数
  3. EDNS
  4. file以及文件大小转化问题
  5. Java 字符串用逗号并接
  6. HTML+CSS学习笔记(8)- CSS选择器
  7. 常用Python第三方库 简介
  8. flume 以 kafka 为channel 的配置
  9. CTF 和 PHP ,数据库
  10. .NET框架 - NETCORE部署IIS
  11. Lcd(一)显示原理
  12. [20181225]12CR2 SQL Plan Directives.txt
  13. Jlink使用技巧之读取STM32内部的程序
  14. IDAPython脚本之收集函数的调用信息
  15. 8 -- 深入使用Spring -- 6... Spring的事务
  16. GreenPlum 初始化配置报错:gpadmin-[ERROR]:-[Errno 12] Cannot allocate memory
  17. list string 互转
  18. redis缓存工具类,提供序列化接口
  19. Dataguard中日志传输服务
  20. erlang开发环境配置

热门文章

  1. vi编辑器的使用(2)
  2. Python复习 基础知识
  3. CSS元素:clip属性作用说明
  4. HDU-2616
  5. set和multiset容器
  6. 生产环境下Flask项目目录构建
  7. 1 python----pycharm本地部署spark
  8. Srping MVC入门推荐
  9. 1090 Highest Price in Supply Chain (25 分)
  10. C++ STL map使用