有人說,程式員很安靜,但我不完全同意,程式員的聒噪,是藏在代碼後面,是藏在程式後面。
這篇文章是android開發人員的必備知識,是我特別為大家整理和總結的,不求完美,但是有用。
1.訊息推送機制
伺服器器端需要變被動為主動,通知客戶一些開發商認為重要的資訊,無論應用程式是否正在運行或者關閉。
我想到了一句話:Don't call me,i will call you!
QQ今天在右下角彈出了一個對話方塊:"奧巴馬宣布本拉登掛了...",正是如此。
自作聰明,就會帶點小聰明,有人喜歡就有人討厭。
2.獨立進程
無論程式是否正在運行,我們都要能通知到客戶,我們需要一個獨立進程的後台服務。
我們需要一個獨立進程的後台服務。
在AndroidManifest.xml中註冊Service時,有一個android:process屬性,如果這個屬性以"."開頭,則為此服務開啟一個全域的獨立進程,如果以":"開頭則為此服務開啟一個為此應用私人的獨立進程。舉個具體的例子吧,我們建立了一個 Application,建立了主進程com.cnblogs.tianxia,那麼:
<!--下面會建立一個全域的com.cnblogs.tianxia.message的獨立進程--><service android:name=".service.MessageService" android:label="訊息推送" android:process=".message" /><!--或者--><!--下面會建立一個應用私人的com.cnblogs.tianxia:message的獨立進程--><service android:name=".service.MessageService" android:label="訊息推送" android:process=":message" />
我們沒必要建立一個全域的,本文選擇第二種方案,建立一個當前應用私人的獨立進程。
3.通知使用者和點擊查看
public class MessageService extends Service {//擷取訊息線程 private MessageThread messageThread = null; //點擊查看 private Intent messageIntent = null; private PendingIntent messagePendingIntent = null; //通知欄訊息 private int messageNotificationID = 1000; private Notification messageNotification = null; private NotificationManager messageNotificatioManager = null; public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { //初始化 messageNotification = new Notification(); messageNotification.icon = R.drawable.icon; messageNotification.tickerText = "新訊息"; messageNotification.defaults = Notification.DEFAULT_SOUND; messageNotificatioManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); messageIntent = new Intent(this, MessageActivity.class); messagePendingIntent = PendingIntent.getActivity(this,0,messageIntent,0); //開啟線程 messageThread = new MessageThread(); messageThread.isRunning = true; messageThread.start(); return super.onStartCommand(intent, flags, startId); } /** * 從伺服器端擷取訊息 * */ class MessageThread extends Thread{ //運行狀態,下一步驟有大用 public boolean isRunning = true; public void run() { while(isRunning){ try { //休息10分鐘 Thread.sleep(600000); //擷取伺服器訊息 String serverMessage = getServerMessage(); if(serverMessage!=null&&!"".equals(serverMessage)){ //更新通知欄 messageNotification.setLatestEventInfo(MessageService.this, "新訊息","奧巴馬宣布,本拉登兄弟掛了!"+serverMessage,messagePendingIntent); messageNotificatioManager.notify(messageNotificationID, messageNotification); //每次通知完,通知ID遞增一下,避免訊息覆蓋掉 messageNotificationID++; } } catch (InterruptedException e) { e.printStackTrace(); } } } } /** * 這裡以此方法為伺服器Demo,僅作樣本 * @return 返回伺服器要推送的訊息,否則如果為空白的話,不推送 */ public String getServerMessage(){ return "YES!"; }}
其中MessageActivity是點擊跳轉的activity,負責處理查看詳細資料。
我們在其他Activity中調用一下:
boolean isMessagePush = true;//不開啟就設定為false;...if(isMessagePush){ startService(new Intent(this, MessageService.class))};
運行一下:
4.停止服務
stopService(new Intent(MyActivity.this,MessageService.class));setMessagePush(false);//設定設定檔或資料庫中flag為false
運行一下,停止服務後,卻出乎意料的並沒有停下來,怎麼回事?是不是代碼寫錯了?
代碼沒有錯,錯在我們停止了服務,卻沒有停止進程,退出線程。
5.退出線程
實踐證明,Thread的stop()方法並不可靠。但是我們有其他的辦法。
在代碼面前,程式員就是上帝。
退出線程有兩種方法。
第一種方法,強制退出。
//殺死該線程所在的進程,自然就退出了System.exit(0);
第二種方法,設定isRunning為false。
//前面說到了isRunning這個標誌,設定為false後,線程的執行就從while迴圈中跳出來了,然後自然結束掉了messageThread.isRunning = false;
綜合一下,我們在MessageService中重載onDestroy()方法如下:
@Overridepublic void onDestroy() { System.exit(0); //或者,二選一,推薦使用System.exit(0),這樣進程退出的更乾淨 //messageThread.isRunning = false; super.onDestroy();}
好了,現在無論是手動停止,還是從工作管理員中強制停止Service,Message Service和訊息線程都能正常的停止和退出了。
我想我已經清楚了說明了訊息推送機制的實現原理,覺得好的話,各位同道,支援一下!