李海平 陳榮征 李海文
(廣東職業(yè)技術(shù)學(xué)院信息工程系,廣東 佛山 528041)
基于線程復(fù)用的Android校園助手的通知模塊設(shè)計(jì)
李海平 陳榮征 李海文
(廣東職業(yè)技術(shù)學(xué)院信息工程系,廣東 佛山 528041)
校園助手項(xiàng)目是基于Android系統(tǒng)開發(fā)的應(yīng)用項(xiàng)目,通知消息模塊是校園助手項(xiàng)目的重要模塊。校園助手項(xiàng)目采用線程復(fù)用技術(shù),訪問網(wǎng)絡(luò),查詢服務(wù)端通知消息數(shù)據(jù),從而減少不斷開啟新線程的開銷,提高資源的使用效率;項(xiàng)目中的通知在Android Service組件中進(jìn)行處理,Service組件中開啟線程訪問網(wǎng)絡(luò),對(duì)網(wǎng)絡(luò)返回的數(shù)據(jù),通過Handler異步消息機(jī)制派發(fā),然后在通知欄發(fā)出通知。這些技術(shù)的應(yīng)用對(duì)設(shè)計(jì)類似的通知消息機(jī)制有廣泛的參考價(jià)值。
Android;校園助手;線程復(fù)用;通知模塊
隨著智能手機(jī)的發(fā)展,現(xiàn)在基本上人手一臺(tái)智能手機(jī),如何讓校園助手更好地服務(wù)學(xué)生,這是我們設(shè)計(jì)校園助手的目的。高校學(xué)生管理中,經(jīng)常需要對(duì)重要的事情發(fā)出通知,一般通知都是按照一級(jí)一級(jí)傳達(dá)的,最后傳達(dá)到每個(gè)學(xué)生需要一段時(shí)間。校園助手針對(duì)這樣的需求,設(shè)計(jì)出自己的消息通知模塊,當(dāng)有重要事情要通知相關(guān)的學(xué)生的時(shí)候,校園助手Android客戶端能接收到相關(guān)消息,并且及時(shí)在學(xué)生手機(jī)端提醒,就像手機(jī)來電一樣,非常方便學(xué)生的管理。
2.1 Android Notification通知結(jié)構(gòu)
Notification有震動(dòng)、鈴聲、文字等多種表現(xiàn)形式,一般情況下默認(rèn)顯示大圖標(biāo)、標(biāo)題、內(nèi)容、時(shí)間、小圖標(biāo)等信息,如圖1。
圖1 文本Notification內(nèi)容
2.2 創(chuàng)建一條消息欄Notification通知
Android創(chuàng)建一條消息通知,首先需要獲取一個(gè)全局的通知欄管理類NotificationManager[1]對(duì)象,notifyManager= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
然后通過NotificationCompat.Builder類對(duì)象創(chuàng)建具體Notification對(duì)象,設(shè)置Notification通知對(duì)象的LargeIcon大圖標(biāo)、Title標(biāo)題、Text內(nèi)容、When時(shí)間、Number數(shù)字、SmallI-con小圖標(biāo),這些通知設(shè)置好之后,調(diào)用Builder對(duì)象的build方法,創(chuàng)建一個(gè)Notification對(duì)象。
NotificationCompat.Builder builder=new Notification-Compat.Builder(this);
builder.setContentTitle("通知標(biāo)題");
builder.setContentText("通知內(nèi)容");
builder.setWhen(System.currentTimeMillis());
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setNumber(9);
builder.setLargeIcon(BitmapFactory.decodeResource(get-Resources(),R.drawable.big));
Intent intent4Pending=new Intent(this,NotificationActivity.class);
PendingIntentpendingIntent=PendingIntent.getActivity(this,0,intent4Pending,0);
builder.setContentIntent(pendingIntent);
Notification notification=builder.build();
有了Notification對(duì)象之后,通過NotificationManager對(duì)象notifyManager發(fā)出一條Notification通知,這樣消息欄就能顯示出一條通知消息。
notifyManager.notify(notificationID,notification);
Service[2]服務(wù)組件是Android的四大組件之一,是在后臺(tái)執(zhí)行的程序,其用于長時(shí)間在后臺(tái)運(yùn)行的邏輯代碼。校園助手項(xiàng)目消息通知模塊,需要在后臺(tái)不斷地訪問網(wǎng)絡(luò),檢查服務(wù)端是否有相關(guān)的通知消息,這些功能代碼都需要在后臺(tái)完成,并且需要一直在后臺(tái)運(yùn)行,所以消息通知模塊采用Service組件實(shí)現(xiàn)。
圖2 Service生命周期
一個(gè)Android Service服務(wù)生命周期如圖2所示,服務(wù)可以在Activity界面層調(diào)用startService啟動(dòng)一個(gè)服務(wù),啟動(dòng)服務(wù)后,自動(dòng)執(zhí)行Oncreate方法,服務(wù)在后臺(tái)運(yùn)行,直到Activity界面層調(diào)用了stopService方法,服務(wù)停止。校園手機(jī)助手啟動(dòng)服務(wù)后,在Oncreate方法中啟動(dòng)線程,啟動(dòng)的線程中不斷訪問網(wǎng)絡(luò),查詢是否有通知消息,如果有通知消息就發(fā)出一條通知顯示在手機(jī)的通知欄。
Android中訪問網(wǎng)絡(luò)需要開啟子線程來完成,如果每訪問一次網(wǎng)絡(luò)就開啟一個(gè)線程,這需要Android操作系統(tǒng)不斷地獲取底層資源,從而影響其它App軟件的使用效率和速度。在校園助手項(xiàng)目中,其查詢服務(wù)端通知消息數(shù)據(jù)是通過開啟一個(gè)子線程,然后復(fù)用這個(gè)子線程來進(jìn)行,每訪問一次網(wǎng)絡(luò),休眠一段時(shí)間再訪問網(wǎng)絡(luò),對(duì)訪問網(wǎng)絡(luò)獲取的數(shù)據(jù)進(jìn)行異步處理,這樣就避免了不斷開啟新的線程訪問網(wǎng)絡(luò),節(jié)約了資源,提高了系統(tǒng)效率。
圖3 線程復(fù)用
如圖3,服務(wù)端啟動(dòng)線程后,先訪問服務(wù)端數(shù)據(jù),如果有數(shù)據(jù)就異步發(fā)出消息,發(fā)出一條Notification顯示在Android通知欄上,否則進(jìn)入睡眠延時(shí),等待下一個(gè)循環(huán)。下面是相關(guān)的線程run函數(shù)代碼實(shí)現(xiàn):
public void run(){
do{
String notifResult="";
notifResult=getNotificationData();//請求服務(wù)端數(shù)據(jù)
if(!notifResult.equals("")){
if(handler!=null){
Message msg=Message.obtain();
msg.what=100;
msg.obj=notifResult;
handler.sendMessage(msg);//異步消息,請求發(fā)出Notification
}
}
try{
Thread.sleep(mLoopTime);//線程休眠
}catch(InterruptedException e){
e.printStackTrace();
}
}while(!bLoop);
}
校園助手項(xiàng)目通知模塊通過在Android Service組件中,利用Thread線程復(fù)用技術(shù),訪問網(wǎng)絡(luò)查詢服務(wù)端通知消息數(shù)據(jù),通過Handler異步消息機(jī)制,異步處理消息數(shù)據(jù),在Android手機(jī)通知欄中發(fā)出通知。本文搭建了Android通知消息模塊的框架,還有不少需要完善的地方,但是有了這樣消息通知框架,對(duì)設(shè)計(jì)類似的消息通知模塊有重要的參考價(jià)值。
[1]http://developer.android.com/design/patterns/notifications.html
[2]傳智播客高教產(chǎn)品研發(fā)部.Android移動(dòng)應(yīng)用基礎(chǔ)教程[M].北京:中國鐵道出版社,2014.
Notification Message Module Design forAndroid CampusAssistant Based on Thread Reuse
Li Haiping Chen Rongzheng Li Haiwen
(Information Engineering Department of Guangdong Polytechnic,Foshan 528041,Guangdong)
The campus assistant project is based on the Android system development application project,and the Notification Message Module is an important module for the Campus Assistant Project.The Campus Assistant Project uses the thread reuse technology,accesses the network,and queries notification data in the service,thus reduces the expense of opening new thread,and enhances the resource efficiency.The notifications in the project are processed in the Android Service component.The Service Component opens thread to access the network.The data returned by the network is distributed with the Handler asynchronous message mechanism,and then the notification is send in the notification bar.The application of these techniques has a wide range of reference values for designing similar notification message mechanisms.
Android;campus assistant;thread reuse;notification module
TP311.52
A
1008-6609(2016)12-0019-02
李海平(19 82-),男,江西寧都人,碩士,研究方向?yàn)橐苿?dòng)應(yīng)用開發(fā)、軟件技術(shù)。
2015年度廣東大學(xué)生科技創(chuàng)新培育專項(xiàng),2016年度廣東職業(yè)技術(shù)學(xué)院教學(xué)改革項(xiàng)目,項(xiàng)目編號(hào):XJJG 2016 08。