首页 / 操作系统 / Linux / Android开机自启动并接收推送消息
首先是Android开机自启动的功能实现,代码如下: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.linuxidc.com/Linux/2015-01/112318.htm相关阅读:在Ubuntu 11.04上安装ZeroMQ http://www.linuxidc.com/Linux/2011-07/38496.htmUbuntu Server 12.10 上安装 Node.js, ZeroMQ http://www.linuxidc.com/Linux/2012-11/73364.htmLinux下ZeroMQ write函数变更 http://www.linuxidc.com/Linux/2013-03/80272.htmZeroMQ使用注意点滴 http://www.linuxidc.com/Linux/2013-03/80273.htm更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-01/112317.htm