设为首页 加入收藏

TOP

Android开机自启动并接收推送消息
2015-02-02 14:11:19 来源: 作者: 【 】 浏览:20
Tags:Android 开机 启动 接收 推送 消息

1.?AndroidManifest.xml中添加如下代码:





…..
? ? ? ? ? ? ? ? android:name="com.demo.notification.NotificationService"
? ? ? ? ? ? android:icon="@drawable/icon"
? ? ? ? ? ? android:label="@string/app_name" >
? ?


? ?
? ? ? ?
? ? ? ? ? ?
? ? ? ?

? ?


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 {


? ? ? ? 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


相关阅读


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android:创建ZeroMQ的客户端 下一篇Clojure:通过ZeroMQ推送消息

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: