接收推送消息部分我们通过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:开机自启动并接收推送消息的更多相关文章

  1. iOS监听模式系列之推送消息通知

    推送通知 和本地通知不同,推送通知是由应用服务提供商发起的,通过苹果的APNs(Apple Push Notification Server)发送到应用客户端.下面是苹果官方关于推送通知的过程示意图: ...

  2. Android APP切换到后台接收不到推送消息

    1.   Android端进程被杀死后,目前自带的保护后台接收消息活跃机制.暂时没有什么好的机制保持任何情况下都活跃 android原生系统用home键杀进程可以起来,如果是强行停止就只能用户自己手动 ...

  3. Android应用实现Push推送消息原理

            本文介绍在Android中实现推送方式的基础知识及相关解决方案.推送功能在手机开发中应用的场景是越来起来了,不说别的,就我 们手机上的新闻客户端就时不j时的推送过来新的消息,很方便的阅 ...

  4. android不需要Socket的跨进程推送消息AIDL!

    上篇介绍了跨进程实时通讯http://www.cnblogs.com/xiaoxiaing/p/5818161.html 但是他有个缺点就是服务端无法推送消息给客户端,今天这篇文章主要说的就是服务器推 ...

  5. Android高效率编码-第三方SDK详解系列(二)——Bmob后端云开发,实现登录注册,更改资料,修改密码,邮箱验证,上传,下载,推送消息,缩略图加载等功能

    Android高效率编码-第三方SDK详解系列(二)--Bmob后端云开发,实现登录注册,更改资料,修改密码,邮箱验证,上传,下载,推送消息,缩略图加载等功能 我的本意是第二篇写Mob的shareSD ...

  6. Android为TV端助力 不需要Socket的跨进程推送消息AIDL!

    上篇介绍了跨进程实时通讯http://www.cnblogs.com/xiaoxiaing/p/5818161.html 但是他有个缺点就是服务端无法推送消息给客户端,今天这篇文章主要说的就是服务器推 ...

  7. Android push推送消息到达成功率优化

    Android push推送消息到达成功率优化 问题:server向client发送消息.未考虑client是否在线,这种消息到达率是非常低的. 第一次优化:使用server离线缓存数据,推断假设cl ...

  8. 使用PushSharp给iOS应用推送消息

    PushSharp是一个C#编写的服务端类库,用于推送消息到各种客户端,支持iOS(iPhone/iPad).Android.Windows Phone.Windows 8.Amazo.Blackbe ...

  9. 使用极光推送(www.jpush.cn)向安卓手机推送消息【服务端向客户端主送推送】C#语言

    在VisualStudio2010中新建网站JPushAndroid.添加引用json帮助类库Newtonsoft.Json.dll. 在web.config增加appkey和mastersecret ...

随机推荐

  1. Akka源码分析-Remote-网络链接生命周期

    remote模式下,网络链接的生命周期往往影响着对应Actor的生命周期,那么网络链接的生命周期是怎么样的呢? 每一个与远程系统的链路都是四个状态之一:空闲.活跃.被守护.被隔离.远程系统的某个地址没 ...

  2. BZOJ 3456 NTT图的计数 容斥

    思路: RT 懒得写了 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm&g ...

  3. Coursera公开课-Machine_learing:编程作业3

    第四周 编程作业: Multi-class Classification and Neural Networks 这周作业与上一周有许多相同的部分,比如longistic regression中的lr ...

  4. html5+css3杂记

    H5C3个人笔记 before&after 1. 必须有content display 2. 场景:不想改变html结构:解决浮动 解决浮动: 2c d h v transition 过渡 1 ...

  5. fcc jQuery 练习

    在页面顶端增加一行script元素,然后写上结束符, 浏览器会运行script 里所有的Javascript,包括jQuery <script>$(document).ready(func ...

  6. JS——delete

    1.对象属性删除 <script> function fun(){ this.name = 'mm'; } var obj = new fun(); console.log(obj.nam ...

  7. ubuntu下sudo命令不能使用问题

    不知道从什么时候开始,ctrl+alt+F1进入命令行之后,登录成功.使用sudo命令,不能使用....被坑了很久. 解决方法: 出现 [sudo ] username !!! 之后,在输入一遍 密码 ...

  8. logstash windows下添加服务启动管理

    nssm下载链接:http://nssm.cc/release/nssm-2.24.zip

  9. css nth-child 的应用

    最近改视频监控页面,由于窗口比较多,以前是通过计算窗口大小位置来处理页面布局的,其实还是比较麻烦,而且偶尔会有页面位置错乱的现象,虽然只是及其偶尔的现象,但总归是不好. 计算窗口位置的代码: /*监控 ...

  10. 微信小程序video监测播放进度

    video组件提供的进度相关的监测只有 bindtimeupdate ,官方说明这个函数250ms触发一次,在开发者工具上基本符合,但在真机上每隔1秒触发一次.达不到我们要求的精度.对比下音频,wx. ...