首先是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
相关阅读:
在Ubuntu 11.04上安装ZeroMQ

