Android中利用App實(shí)現(xiàn)消息推送機(jī)制的代碼
更新時(shí)間:2011年05月07日 20:33:13 作者:
Android中利用App實(shí)現(xiàn)消息推送機(jī)制的代碼,需要的朋友可以參考下。
1.消息推送機(jī)制
服務(wù)器器端需要變被動為主動,通知客戶一些開發(fā)商認(rèn)為重要的信息,無論應(yīng)用程序是否正在運(yùn)行或者關(guān)閉。
我想到了一句話:don't call me,i will call you!
qq今天在右下角彈出了一個(gè)對話框:"奧巴馬宣布本拉登掛了...",正是如此。
自作聰明,就會帶點(diǎn)小聰明,有人喜歡就有人討厭。
2.獨(dú)立進(jìn)程
無論程序是否正在運(yùn)行,我們都要能通知到客戶,我們需要一個(gè)獨(dú)立進(jìn)程的后臺服務(wù)。
我們需要一個(gè)獨(dú)立進(jìn)程的后臺服務(wù)。
在androidmanifest.xml中注冊service時(shí),有一個(gè)android:process屬性,如果這個(gè)屬性以"."開頭,則為此服務(wù)開啟一個(gè)
全局的獨(dú)立進(jìn)程,如果以":"開頭則為此服務(wù)開啟一個(gè)為此應(yīng)用私有的獨(dú)立進(jìn)程。舉個(gè)具體的例子吧,我們新建了一個(gè)
application,創(chuàng)建了主進(jìn)程com.cnblogs.tianxia,那么:
<!--下面會創(chuàng)建一個(gè)全局的com.cnblogs.tianxia.message的獨(dú)立進(jìn)程-->
<service android:name=".service.messageservice" android:label="消息推送" android:process=".message" />
<!--或者-->
<!--下面會創(chuàng)建一個(gè)應(yīng)用私有的com.cnblogs.tianxia:message的獨(dú)立進(jìn)程-->
<service android:name=".service.messageservice" android:label="消息推送" android:process=":message" />
我們沒必要建立一個(gè)全局的,本文選擇第二種方案,創(chuàng)建一個(gè)當(dāng)前應(yīng)用私有的獨(dú)立進(jìn)程。
3.通知用戶和點(diǎn)擊查看
public class messageservice extends service {
//獲取消息線程
private messagethread messagethread = null;
//點(diǎn)擊查看
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);
}
/**
* 從服務(wù)器端獲取消息
*
*/
class messagethread extends thread{
//運(yùn)行狀態(tài),www.3ppt.com下一步驟有大用
public boolean isrunning = true;
public void run() {
while(isrunning){
try {
//休息10分鐘
thread.sleep(600000);
//獲取服務(wù)器消息
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();
}
}
}
}
/**
* 這里以此方法為服務(wù)器demo,僅作示例
* @return 返回服務(wù)器要推送的消息,否則如果為空的話,不推送
*/
public string getservermessage(){
return "yes!";
}
}
其中messageactivity是點(diǎn)擊跳轉(zhuǎn)的activity,負(fù)責(zé)處理查看詳細(xì)信息。
我們在其他activity中調(diào)用一下:
boolean ismessagepush = true;//不開啟就設(shè)置為false;
...
if(ismessagepush){
startservice(new intent(this, messageservice.class))
};
運(yùn)行一下:
4.停止服務(wù)
1 stopservice(new intent(myactivity.this,messageservice.class));
2 setmessagepush(false);//設(shè)置配置文件或數(shù)據(jù)庫中flag為false
運(yùn)行一下,停止服務(wù)后,卻出乎意料的并沒有停下來,怎么回事?是不是代碼寫錯(cuò)了?
代碼沒有錯(cuò),錯(cuò)在我們停止了服務(wù),卻沒有停止進(jìn)程,退出線程。
5.退出線程
實(shí)踐證明,thread的stop()方法并不可靠。但是我們有其他的辦法。
在代碼面前,程序員就是上帝。
退出線程有兩種方法。
第一種方法,強(qiáng)制退出。
//殺死該線程所在的進(jìn)程,自然就退出了
2 system.exit(0);
第二種方法,設(shè)置isrunning為false。
view sourceprint?1 //前面說到了isrunning這個(gè)標(biāo)志,設(shè)置為false后,線程的執(zhí)行就從while循環(huán)中跳出來了,然后自然結(jié)束
掉了
2 messagethread.isrunning = false;
綜合一下,我們在messageservice中重載ondestroy()方法如下:
@override
public void ondestroy() {
system.exit(0);
//或者,二選一,推薦使用system.exit(0),這樣進(jìn)程退出的更干凈
//messagethread.isrunning = false;
super.ondestroy();
}
服務(wù)器器端需要變被動為主動,通知客戶一些開發(fā)商認(rèn)為重要的信息,無論應(yīng)用程序是否正在運(yùn)行或者關(guān)閉。
我想到了一句話:don't call me,i will call you!
qq今天在右下角彈出了一個(gè)對話框:"奧巴馬宣布本拉登掛了...",正是如此。
自作聰明,就會帶點(diǎn)小聰明,有人喜歡就有人討厭。
2.獨(dú)立進(jìn)程
無論程序是否正在運(yùn)行,我們都要能通知到客戶,我們需要一個(gè)獨(dú)立進(jìn)程的后臺服務(wù)。
我們需要一個(gè)獨(dú)立進(jìn)程的后臺服務(wù)。
在androidmanifest.xml中注冊service時(shí),有一個(gè)android:process屬性,如果這個(gè)屬性以"."開頭,則為此服務(wù)開啟一個(gè)
全局的獨(dú)立進(jìn)程,如果以":"開頭則為此服務(wù)開啟一個(gè)為此應(yīng)用私有的獨(dú)立進(jìn)程。舉個(gè)具體的例子吧,我們新建了一個(gè)
application,創(chuàng)建了主進(jìn)程com.cnblogs.tianxia,那么:
復(fù)制代碼 代碼如下:
<!--下面會創(chuàng)建一個(gè)全局的com.cnblogs.tianxia.message的獨(dú)立進(jìn)程-->
<service android:name=".service.messageservice" android:label="消息推送" android:process=".message" />
<!--或者-->
<!--下面會創(chuàng)建一個(gè)應(yīng)用私有的com.cnblogs.tianxia:message的獨(dú)立進(jìn)程-->
<service android:name=".service.messageservice" android:label="消息推送" android:process=":message" />
我們沒必要建立一個(gè)全局的,本文選擇第二種方案,創(chuàng)建一個(gè)當(dāng)前應(yīng)用私有的獨(dú)立進(jìn)程。
3.通知用戶和點(diǎn)擊查看
public class messageservice extends service {
//獲取消息線程
private messagethread messagethread = null;
//點(diǎn)擊查看
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);
}
/**
* 從服務(wù)器端獲取消息
*
*/
class messagethread extends thread{
//運(yùn)行狀態(tài),www.3ppt.com下一步驟有大用
public boolean isrunning = true;
public void run() {
while(isrunning){
try {
//休息10分鐘
thread.sleep(600000);
//獲取服務(wù)器消息
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();
}
}
}
}
/**
* 這里以此方法為服務(wù)器demo,僅作示例
* @return 返回服務(wù)器要推送的消息,否則如果為空的話,不推送
*/
public string getservermessage(){
return "yes!";
}
}
其中messageactivity是點(diǎn)擊跳轉(zhuǎn)的activity,負(fù)責(zé)處理查看詳細(xì)信息。
我們在其他activity中調(diào)用一下:
復(fù)制代碼 代碼如下:
boolean ismessagepush = true;//不開啟就設(shè)置為false;
...
if(ismessagepush){
startservice(new intent(this, messageservice.class))
};
運(yùn)行一下:
4.停止服務(wù)
1 stopservice(new intent(myactivity.this,messageservice.class));
2 setmessagepush(false);//設(shè)置配置文件或數(shù)據(jù)庫中flag為false
運(yùn)行一下,停止服務(wù)后,卻出乎意料的并沒有停下來,怎么回事?是不是代碼寫錯(cuò)了?
代碼沒有錯(cuò),錯(cuò)在我們停止了服務(wù),卻沒有停止進(jìn)程,退出線程。
5.退出線程
實(shí)踐證明,thread的stop()方法并不可靠。但是我們有其他的辦法。
在代碼面前,程序員就是上帝。
退出線程有兩種方法。
第一種方法,強(qiáng)制退出。
//殺死該線程所在的進(jìn)程,自然就退出了
2 system.exit(0);
第二種方法,設(shè)置isrunning為false。
view sourceprint?1 //前面說到了isrunning這個(gè)標(biāo)志,設(shè)置為false后,線程的執(zhí)行就從while循環(huán)中跳出來了,然后自然結(jié)束
掉了
2 messagethread.isrunning = false;
綜合一下,我們在messageservice中重載ondestroy()方法如下:
復(fù)制代碼 代碼如下:
@override
public void ondestroy() {
system.exit(0);
//或者,二選一,推薦使用system.exit(0),這樣進(jìn)程退出的更干凈
//messagethread.isrunning = false;
super.ondestroy();
}
相關(guān)文章
Android Studio實(shí)現(xiàn)進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了Android Studio實(shí)現(xiàn)進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
基于Android實(shí)現(xiàn)定時(shí)刷新功能
定時(shí)刷新是一種常見的應(yīng)用需求,例如自動加載新數(shù)據(jù)、定時(shí)更新 UI、動畫循環(huán)播放、實(shí)時(shí)監(jiān)控等場景中都需要定時(shí)刷新頁面,Android 平臺提供了多種實(shí)現(xiàn)定時(shí)刷新的方式,本文將結(jié)合實(shí)例詳細(xì)講解如何實(shí)現(xiàn)定時(shí)刷新功能,需要的朋友可以參考下2025-04-04
在Android中實(shí)現(xiàn)浮窗并添加吸邊效果的代碼示例
在 Android 中實(shí)現(xiàn)浮窗(懸浮窗)并添加吸邊效果,可以使用 WindowManager 來管理浮窗視圖,并通過觸摸事件來實(shí)現(xiàn)吸邊效果,以下是一個(gè)示例,展示如何創(chuàng)建一個(gè)浮窗并實(shí)現(xiàn)吸邊效果,需要的朋友可以參考下2025-02-02
Android開發(fā)實(shí)現(xiàn)橫向列表GridView橫向滾動的方法【附源碼下載】
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)橫向列表GridView橫向滾動的方法,結(jié)合實(shí)例形式分析了Android橫向列表GridView實(shí)現(xiàn)橫向滾動的相關(guān)布局與功能實(shí)現(xiàn)技巧,并附帶源碼供讀者下載參考,需要的朋友可以參考下2018-01-01
Android通過多點(diǎn)觸控的方式對圖片進(jìn)行縮放的實(shí)例代碼
這篇文章主要介紹了Android通過多點(diǎn)觸控的方式對圖片進(jìn)行縮放的實(shí)例代碼,完成了點(diǎn)擊圖片就能瀏覽大圖的功能,并且在瀏覽大圖的時(shí)候還可以通過多點(diǎn)觸控的方式對圖片進(jìn)行縮放。2018-05-05
Android布局中margin與padding的區(qū)別及說明
這篇文章主要介紹了Android布局中margin與padding的區(qū)別及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Android實(shí)現(xiàn)志愿者系統(tǒng)詳細(xì)步驟與代碼
這篇文章主要介紹了Android實(shí)現(xiàn)志愿者系統(tǒng),本系統(tǒng)采用MVC架構(gòu)設(shè)計(jì),SQLite數(shù)據(jù)表有用戶表、成員表和活動表,有十多個(gè)Activity頁面。打開應(yīng)用,進(jìn)入歡迎界面,3s后跳轉(zhuǎn)登錄界面,用戶先注冊賬號,登錄成功后進(jìn)入主界面2023-02-02

