Android 开机启动服务
在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);
}
Android 开机启动服务的更多相关文章
- android开机启动应用和服务
注冊广播监听开机状态.启动应用和服务等: 监听开机的广播接收器: public class BootCompletedReceiver extends BroadcastReceiver{ @Over ...
- android开机启动流程说明
android开机启动流程说明 第一步:启动linux 1.Bootloader 2.Kernel 第二步android系统启动:入口为init.rc(system\core\rootdir) 1./ ...
- Android开机启动Activity或者Service方法
本文出自 “Bill_Hoo专栏” 博客,请务必保留此出处http://billhoo.blog.51cto.com/2337751/761230 这段时间在做Android的基础开发,现在有一需求是 ...
- ANDROID 开机启动VNC SERVER
ANDROID 开机启动VNC SERVER 背景信息: 最近在做一个项目,在项目需求中有这么一项“要把VNC SERVER 添加到android里并让其开机自启动”.其实做这个项目也挺缚手缚脚的,因 ...
- Hortonworks HDP Sandbox定制(配置)开机启动服务(组件)
定制Hortonworks HDP开机启动服务能够这样做:本文原文出处: http://blog.csdn.net/bluishglc/article/details/42109253 严禁不论什么形 ...
- Android开机启动Activity或者Service方法(转载)
这段时间在做Android的基础开发,现在有一需求是开机启动,按照网上某些博文教程做了下,始终不成功,一开机总是提示所启动的应用程序意外终止,于是参考了Android SDK doc,终于解决问题,下 ...
- Centos7.x:开机启动服务的配置和管理
一.开机启动服务的配置 1.创建服务配置(权限754) vim /usr/lib/systemd/system/nginx.service 文件内容解释 [Unit]:服务的说明Description ...
- linux chkconfig添加开机启动服务
--add:增加所指定的系统服务,让chkconfig指令得以管理它,并同时在系统启动的叙述文件内增加相关数据: --del:删除所指定的系统服务,不再由chkconfig指令管理,并同时在系统启动的 ...
- linux自定义开机启动服务和chkconfig使用方法
linux自定义开机启动服务和chkconfig使用方法 1. 服务概述在linux操作系统下,经常需要创建一些服务,这些服务被做成shell脚本,这些服务需要在系统启动的时候自动启动,关闭的时候自动 ...
随机推荐
- Eigen中的noalias(): 解决矩阵运算的混淆问题
作者:@houkai本文为作者原创,转载请注明出处:http://www.cnblogs.com/houkai/p/6349990.html 目录 混淆例子解决混淆问题混淆和component级的操作 ...
- cocos2dx 新手引导
static CCClippingNode* create(); //使用一个节点作为模版创建裁剪节点 static CCClippingNode* create(CCNode *pStencil); ...
- http协议之版本差异(2)
—————————————HTTP1.0/HTTP1.1—————————————— 建立连接方面 HTTP/1.0 每次请求都需要建立新的TCP连接,连接不能复用.HTTP/1.1 新的请求可以在上 ...
- 分解gif图片并保存
/** Gif的步骤 1. 拿到Gifd的数据 2. 将Gif分解为一帧帧 3. 将单帧数据转为UIImage 4. 单帧图片保存 */ #import <ImageIO/ImageIO.h&g ...
- C++设计模式之工厂方法模式
来自:http://blog.csdn.net/pangshaohua/article/details/38912555 参考写的一个工厂demo 1.定义"背景风格的抽象类".& ...
- UVALive 7327【模拟】
题意: 每次方案一个或多个子序列: 每个子序列要整除m 认为分割不同,子序列边界的不同就是不同: 1246有4个 1246 12 46 124 6 12 4 6 思路: 先从整体考虑,因为取膜适用于加 ...
- Ogre 学习记录
http://www.cppblog.com/richardhe/articles/55722.html 1: 设计初衷 它设计初衷是完全跨平台的.抽象的接口隐藏了平台相关的细节. 它设计初衷是大幅度 ...
- Educational Codeforces Round 19 A, B, C, E(xjb)
题目链接:http://codeforces.com/contest/797 A题 题意:给出两个数n, k,问能不能将n分解成k个因子相乘的形式,不能输出-1,能则输出其因子: 思路:将n质因分解, ...
- Html5shiv ---- 让IE低版本浏览器识别并支持HTML5标签
Html5shiv.js是针对IE浏览器的 javaScript 补丁,作用如题 该脚本的下载链接 使用使在head标签中使用script标签引用即可
- VRTK3.3.0-004传送
直线传送: 一.无高度变换传送(VRTK_BasicTeleport) 1丶继续在VRScripts下创建空物体PlayArea,用来挂在传送相关脚本:创建Plane作为传送地面 2丶在PlayAre ...