Android:开机自启动并接收推送消息
接收推送消息部分我们通过ZeroMQ实现,可以参考http://www.cnblogs.com/ilovewindy/p/3984283.html。
首先是开机自启动的功能实现,代码如下:
1. AndroidManifest.xml中添加如下代码:
<!-- 抓取系统启动事件 -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application>
…..
<service
android:name="com.demo.notification.NotificationService"
android:icon="@drawable/icon"
android:label="@string/app_name" >
</service> <receiver android:name="com.demo.notification.MyScheduleReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
2. 接着是实现MyScheduleReceiver代码,这是当Android启动后会自动启动的程序。
public class MyScheduleReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, NotificationService.class);
context.startService(service);
}
}
3. 实现NotificationService代码,用来接收推送消息
public class NotificationService extends Service {
private final IBinder mBinder = new MyBinder();
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 将接收推送消息任务放入后台执行
new ZeroMQMessageTask().execute();
return Service.START_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
return mBinder;
}
public class MyBinder extends Binder {
public NotificationService getService() {
return NotificationService.this;
}
}
private class ZeroMQMessageTask extends AsyncTask<String, Void, String> {
public ZeroMQMessageTask() {
}
@Override
protected String doInBackground(String... params) {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket subscriber = context.socket(ZMQ.SUB);
subscriber.subscribe(ZMQ.SUBSCRIPTION_ALL);
subscriber.connect("tcp://x.x.x.x:xxxx");
while (true) { // 通过不终止的循环来保证接收消息
message = subscriber.recvStr();
if (!message.equals("0")) { // 0是由我自己定义的空消息标识,可以替换成自定义的其它标识
// 显示推送消息
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.icon;
CharSequence tickerText = "Demo - " + message;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon,
tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Context uiContext = getApplicationContext();
CharSequence contentTitle = "Demo";
CharSequence contentText = message;
Intent notificationIntent = new Intent(uiContext,
NotificationService.class);
PendingIntent contentIntent = PendingIntent
.getActivity(uiContext, 0, notificationIntent, 0);
notification.setLatestEventInfo(uiContext, contentTitle,
contentText, contentIntent);
mNotificationManager.notify(1, notification);
}
}
}
@Override
protected void onPostExecute(String result) {
}
}
}
好啦,重新启动手机尝试发条消息吧! :D
服务器端的代码可以参考此文:http://www.cnblogs.com/ilovewindy/p/3998862.html
Android:开机自启动并接收推送消息的更多相关文章
- iOS监听模式系列之推送消息通知
推送通知 和本地通知不同,推送通知是由应用服务提供商发起的,通过苹果的APNs(Apple Push Notification Server)发送到应用客户端.下面是苹果官方关于推送通知的过程示意图: ...
- Android APP切换到后台接收不到推送消息
1. Android端进程被杀死后,目前自带的保护后台接收消息活跃机制.暂时没有什么好的机制保持任何情况下都活跃 android原生系统用home键杀进程可以起来,如果是强行停止就只能用户自己手动 ...
- Android应用实现Push推送消息原理
本文介绍在Android中实现推送方式的基础知识及相关解决方案.推送功能在手机开发中应用的场景是越来起来了,不说别的,就我 们手机上的新闻客户端就时不j时的推送过来新的消息,很方便的阅 ...
- android不需要Socket的跨进程推送消息AIDL!
上篇介绍了跨进程实时通讯http://www.cnblogs.com/xiaoxiaing/p/5818161.html 但是他有个缺点就是服务端无法推送消息给客户端,今天这篇文章主要说的就是服务器推 ...
- Android高效率编码-第三方SDK详解系列(二)——Bmob后端云开发,实现登录注册,更改资料,修改密码,邮箱验证,上传,下载,推送消息,缩略图加载等功能
Android高效率编码-第三方SDK详解系列(二)--Bmob后端云开发,实现登录注册,更改资料,修改密码,邮箱验证,上传,下载,推送消息,缩略图加载等功能 我的本意是第二篇写Mob的shareSD ...
- Android为TV端助力 不需要Socket的跨进程推送消息AIDL!
上篇介绍了跨进程实时通讯http://www.cnblogs.com/xiaoxiaing/p/5818161.html 但是他有个缺点就是服务端无法推送消息给客户端,今天这篇文章主要说的就是服务器推 ...
- Android push推送消息到达成功率优化
Android push推送消息到达成功率优化 问题:server向client发送消息.未考虑client是否在线,这种消息到达率是非常低的. 第一次优化:使用server离线缓存数据,推断假设cl ...
- 使用PushSharp给iOS应用推送消息
PushSharp是一个C#编写的服务端类库,用于推送消息到各种客户端,支持iOS(iPhone/iPad).Android.Windows Phone.Windows 8.Amazo.Blackbe ...
- 使用极光推送(www.jpush.cn)向安卓手机推送消息【服务端向客户端主送推送】C#语言
在VisualStudio2010中新建网站JPushAndroid.添加引用json帮助类库Newtonsoft.Json.dll. 在web.config增加appkey和mastersecret ...
随机推荐
- CSS样式适配杂记
1.问:input框的对齐,制作类似百度搜索框的时候,发现IE下前面输入框和后面按钮总是不能对齐. 解答:给input框增加vertical-align:bottom; 2.问:IE下display: ...
- [Swift通天遁地]五、高级扩展-(1)快速检测设备属性:版本、类型、屏幕尺寸
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- JSP所需要掌握的部分
JSP基本语法 指令 <%@ 指令%> JSP指令是JSP的引擎 主要的两种指令是page和include(taglib) <%@ page import="java.ut ...
- Scala-基础-数据类型
import junit.framework.TestCase import org.junit.Test import scala.runtime.RichByte //数据类型 class Dem ...
- win7 硬盘安装suse双系统启动顺序更改
使用win7硬盘安装suse双系统之后,首先面临的问题是,PC默认启动的系统更改的问题,有些人可能想默认启动是win7,只有在使用linux的时候在去选择suse系统,这里我告诉大家更改的办法: 首先 ...
- SQL基本操作——存储过程
存储过程类似于C#中的方法. --创建存储过程 create proc usp_TwoNumberAdd @num1 int, @num2 int as begin select @num1+@num ...
- 易买网之smartupload实现文件上传
经过俩个星期的奋斗,易买网项目完工.在之前,实现图片的上传,走过许多弯路,原来是好多基础的知识忘记了,没把smartupload文件包添加组件jar包至WEB-INF/lib包中,在此特别重视,做下文 ...
- C/C++ 之数组排序
#include <stdio.h> #include <stdlib.h> void array_sort(int *a, int len) { int i, j, tmp; ...
- PAC代理语法含义与书写规范
一直以来使用ShadowSocksFQ,基本上默认的PAC代理模式己能满足所需,实在个别pac不方便的就转成用全局代理模式也能愉快FQ. 只是最近学习前端的知识,需要FQ访问 MDN web docs ...
- windows 小知识---windows下生成公钥和私钥
首先Windows操作系统需要安装git. 安装完成后,再到任意的文件夹内,点击右键.选择git bash here 打开之后,输入ssh-keygen,一路按enter键. 全部结束后,再到C:\U ...