Android中利用App實現消息推送機制的代碼實例

gequan 11年前發布 | 35K 次閱讀 Android開發 移動開發

Android中利用App實現消息推送機制的代碼實例

1.消息推送機制
     服務器器端需要變被動為主動,通知客戶一些開發商認為重要的信息,無論應用程序是否正在運行或者關閉。
     我想到了一句話:don't call me,i will call you!
     qq今天在右下角彈出了一個對話框:"奧巴馬宣布本拉登掛了...",正是如此。
     自作聰明,就會帶點小聰明,有人喜歡就有人討厭。 

2.獨立進程
     無論程序是否正在運行,我們都要能通知到客戶,我們需要一個獨立進程的后臺服務。
     我們需要一個獨立進程的后臺服務。
     androidmanifest.xml中注冊service時,有一個android:process屬性,如果這個屬性以"."開頭,則為此服務開啟一個

全局的獨立進程,如果以":"開頭則為此服務開啟一個為此應用私有的獨立進程。舉個具體的例子吧,我們新建了一個

application,創建了主進程com.cnblogs.tianxia,那么:

view sourceprint?1 <!--下面會創建一個全局的com.cnblogs.tianxia.message的獨立進程-->

2 <service android:name=".service.messageservice" android:label="消息推送" android:process=".message" /> 

3 <!--或者-->

4 <!--下面會創建一個應用私有的com.cnblogs.tianxia:message的獨立進程-->

5 <service android:name=".service.messageservice" android:label="消息推送" android:process=":message" />

    我們沒必要建立一個全局的,本文選擇第二種方案,創建一個當前應用私有的獨立進程。

3.通知用戶和點擊查看

view sourceprint?01 public class messageservice extends service { 

02   

03     //獲取消息線程 

04     private messagethread messagethread = null; 

05   

06     //點擊查看 

07     private intent messageintent = null; 

08     private pendingintent messagependingintent = null; 

09   

10     //通知欄消息 

11     private int messagenotificationid = 1000; 

12     private notification messagenotification = null; 

13     private notificationmanager messagenotificatiomanager = null; 

14   

15     public ibinder onbind(intent intent) { 

16         return null; 

17     } 

18   

19     @override

20     public int onstartcommand(intent intent, int flags, int startid) { 

21         //初始化 

22         messagenotification = new notification(); 

23         messagenotification.icon = r.drawable.icon; 

24         messagenotification.tickertext = "新消息"; 

25         messagenotification.defaults = notification.default_sound; 

26         messagenotificatiomanager = (notificationmanager)getsystemservice(context.notification_service); 

27   

28         messageintent = new intent(this, messageactivity.class); 

29         messagependingintent = pendingintent.getactivity(this,0,messageintent,0); 

30   

31         //開啟線程 

32         messagethread = new messagethread(); 

33         messagethread.isrunning = true; 

34         messagethread.start(); 

35   

36         return super.onstartcommand(intent, flags, startid);  

37     } 

38       

39     /** 

40      * 從服務器端獲取消息 

41      * 

42      */

43     class messagethread extends thread{ 

44         //運行狀態,www.3ppt.com下一步驟有大用 

45         public boolean isrunning = true; 

46         public void run() { 

47             while(isrunning){ 

48                 try { 

49                     //休息10分鐘 

50                     thread.sleep(600000); 

51                     //獲取服務器消息 

52                     string servermessage = getservermessage(); 

53                     if(servermessage!=null&&!"".equals(servermessage)){ 

54                         //更新通知欄 

55                         messagenotification.setlatesteventinfo(messageservice.this,"新消息","奧巴馬宣布,本拉

登兄弟掛了!"+servermessage,messagependingintent); 

56                         messagenotificatiomanager.notify(messagenotificationid, messagenotification); 

57                         //每次通知完,通知id遞增一下,避免消息覆蓋掉 

58                         messagenotificationid++; 

59                     } 

60                 } catch (interruptedexception e) { 

61                     e.printstacktrace(); 

62                 } 

63             } 

64         } 

65     } 

66   

67     /** 

68      * 這里以此方法為服務器demo,僅作示例 

69      * @return 返回服務器要推送的消息,否則如果為空的話,不推送 

70      */

71     public string getservermessage(){ 

72         return "yes!"; 

73     } 

74 }

  其中messageactivity是點擊跳轉的activity,負責處理查看詳細信息。
  我們在其他activity中調用一下:

view sourceprint?1 boolean ismessagepush = true;//不開啟就設置為false; 

2 ... 

3 if(ismessagepush){ 

4      startservice(new intent(this, messageservice.class)) 

5 };

  運行一下:


4.停止服務

view sourceprint?1 stopservice(new intent(myactivity.this,messageservice.class)); 

2 setmessagepush(false);//設置配置文件或數據庫中flagfalse

    運行一下,停止服務后,卻出乎意料的并沒有停下來,怎么回事?是不是代碼寫錯了?
    代碼沒有錯,錯在我們停止了服務,卻沒有停止進程,退出線程。

5.退出線程
    實踐證明,threadstop()方法并不可靠。但是我們有其他的辦法。
    在代碼面前,程序員就是上帝。
    退出線程有兩種方法。
    第一種方法,強制退出。

view sourceprint?1 //殺死該線程所在的進程,自然就退出了 

2 system.exit(0);

   第二種方法,設置isrunningfalse

view sourceprint?1 //前面說到了isrunning這個標志,設置為false后,線程的執行就從while循環中跳出來了,然后自然結束

掉了 

2 messagethread.isrunning = false;

   綜合一下,我們在messageservice中重載ondestroy()方法如下:

view sourceprint?1 @override

2 public void ondestroy() { 

3             system.exit(0); 

4             //或者,二選一,推薦使用system.exit(0),這樣進程退出的更干凈 

5             //messagethread.isrunning = false; 

6             super.ondestroy(); 

7 }

</div>

 本文由用戶 gequan 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!