接收推送消息部分我们通过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. P3194 [HNOI2008]水平可见直线

    传送门 我们把所有的直线按斜率从小到大排序,然后用单调栈维护 发现,如果当前直线与\(st[top-1]\)直线的交点的横坐标大于等于与\(st[top]\)的交点的横坐标,当前直线可以覆盖掉\(st ...

  2. [Swift通天遁地]九、拔剑吧-(4)使用开源类库创建可滑动的Segment分段控件

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  3. mina2 笔记

    http://www.iteye.com/topic/1112123 http://dongxuan.iteye.com/blog/901689 http://scholers.iteye.com/b ...

  4. HDFS你一定要知道,要考的

    你肯定听过Hadoop,对就是那头奔跑的小象. Hadoop作为大数据时代代表性的解决方案被大家所熟知,它主要包含两部分内容: HDFS分布式文件存储 MapReduce分布式计算框架 前面我们分析存 ...

  5. MVC系列学习(十五)-验证码

    1.方式一: public class VCode { /// <summary> /// 生成验证码图片 字节数组 /// </summary> /// <return ...

  6. iOS - UITableView 单选功能实现

    #import <UIKit/UIKit.h> @interface TestCell : UITableViewCell @property(nonatomic,copy)NSStrin ...

  7. [hihocoder][Offer收割]编程练习赛58

    最大的K-偏差排列 每次取可选范围里的最大的数字,如果最左侧的数字还没有使用就直接使用最左侧的数字 #pragma comment(linker, "/STACK:102400000,102 ...

  8. mybatis中映射文件和实体类的关联性

    mybatis的映射文件写法多种多样,不同的写法和用法,在实际开发过程中所消耗的开发时间.维护时间有很大差别,今天我就把我认为比较简单的一种映射文件写法记录下来,供大家修改建议,争取找到一个最优写法~ ...

  9. iframe监听unload事件

    阻止默认事件 event.preventDefault(); 阻止事件冒泡 event.stopPropagation(); event.cancelBubble = true; //IE <a ...

  10. Zip, Join, GroupJoin

    Zip 合并兩個序列,產生一個新的對象序列,但連接方式是一对一的(即序列1和第一项连接序列2的第一项),所以最终结果会在较短的序列处终止. Zip在這裏不是壓縮的意思,而是拉鏈,意爲連接兩個序列 Pe ...