你好,游客 登录 注册 搜索
背景:
阅读新闻

Android开机自启动并接收推送消息

[日期:2015-01-24] 来源:Linux社区  作者:Linux [字体:大 中 小]

首先是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.htm

Ubuntu Server 12.10 上安装 Node.js, ZeroMQ http://www.linuxidc.com/Linux/2012-11/73364.htm

Linux下ZeroMQ write函数变更 http://www.linuxidc.com/Linux/2013-03/80272.htm

ZeroMQ使用注意点滴 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

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款