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脚本,这些服务需要在系统启动的时候自动启动,关闭的时候自动 ...
随机推荐
- Linux中进程控制块PCB-------task_struct结构体结构
Linux中task_struct用来控制管理进程,结构如下: struct task_struct { //说明了该进程是否可以执行,还是可中断等信息 volatile long state; // ...
- 【HBase】HBase笔记:HBase的Region机制
HBase 的机制里包含了许多优秀的算法,如 Region 定位.Region 分配.Region Server的上线和下线.Master 的上线和下线.在谈到这些之前,先把 HBase 的基本架构里 ...
- python 并发编程之IO 模型
首先说一下 IO 发生时涉及的对象和步骤.以read 为例,会经历两个阶段: 1)等待数据准备 2)将数据从内核拷贝到进程中 二,阻塞Io(blocking IO) 在 Linux中 默认情况下所有 ...
- UVaLive 2965 Jurassic Remains (状态压缩)
题意:给定 n 个大写字母组成的字符串,选择尽量多的串,使得大写字母都能出现偶数次. 析:由于n比较小,我们可以枚举前n/2的所有组合,然后再从后面查找. 代码如下: #pragma comment( ...
- Linux useradd 添加用户
在 linux 中,如果我们想添加一个用户,那么使用的命令如下: 用户管理命令: useradd 基础的命令 命令名称:useradd 命令的所在路径:/usr/bin/useradd 执行权限:ro ...
- ASP.NET Core编程实现基本身份认证
概览 在HTTP中,基本认证(Basic access authentication,简称BA认证)是一种用来允许网页浏览器或其他客户端程序在请求资源时提供用户名和口令形式的身份凭证的一种登录验证方式 ...
- kuangbin带你飞 - 合集
[题目列表] 之前有一些做过了的,这次从数论开始?
- iOS 8 之后的动态沙盒路径
在iOS8之前,我们获取到沙盒中的document.cache.tmp之后,下一次模拟器或真机无论重启多少次,这具体的路径是固定的,可是iOS8 之后,你要是在按原来的路径去找你想要的东西,我想它会把 ...
- lightoj 1074【spfa判负环】
题意: 给你一幅图,dis(u->v)的权值就是(w[v]-w[u])*(w[v]-w[u])*(w[v]-w[u]),所以有可能是负的,给你n个询问,给出最短路,长度<3或者不可达输出& ...
- 51nod1402(贪心)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1402 题意:中文题诶- 思路:贪心 对于一个桩点,如果我们只考 ...