Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中利用App實現消息推送機制的代碼

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

編輯:關於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{
//運行狀態,www.3ppt.com下一步驟有大用
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.停止服務
1 stopservice(new intent(myactivity.this,messageservice.class));
2 setmessagepush(false);//設置配置文件或數據庫中flag為false
運行一下,停止服務後,卻出乎意料的並沒有停下來,怎麼回事?是不是代碼寫錯了?
代碼沒有錯,錯在我們停止了服務,卻沒有停止進程,退出線程。
5.退出線程
實踐證明,thread的stop()方法並不可靠。但是我們有其他的辦法。
在代碼面前,程序員就是上帝。
退出線程有兩種方法。
第一種方法,強制退出。
//殺死該線程所在的進程,自然就退出了
2 system.exit(0);
第二種方法,設置isrunning為false。
view sourceprint?1 //前面說到了isrunning這個標志,設置為false後,線程的執行就從while循環中跳出來了,然後自然結束
掉了
2 messagethread.isrunning = false;
綜合一下,我們在messageservice中重載ondestroy()方法如下:
復制代碼 代碼如下:
@override
public void ondestroy() {
system.exit(0);
//或者,二選一,推薦使用system.exit(0),這樣進程退出的更干淨
//messagethread.isrunning = false;
super.ondestroy();
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved