李海平 陳榮征 李海文
(廣東職業技術學院信息工程系,廣東 佛山 528041)
基于線程復用的Android校園助手的通知模塊設計
李海平 陳榮征 李海文
(廣東職業技術學院信息工程系,廣東 佛山 528041)
校園助手項目是基于Android系統開發的應用項目,通知消息模塊是校園助手項目的重要模塊。校園助手項目采用線程復用技術,訪問網絡,查詢服務端通知消息數據,從而減少不斷開啟新線程的開銷,提高資源的使用效率;項目中的通知在Android Service組件中進行處理,Service組件中開啟線程訪問網絡,對網絡返回的數據,通過Handler異步消息機制派發,然后在通知欄發出通知。這些技術的應用對設計類似的通知消息機制有廣泛的參考價值。
Android;校園助手;線程復用;通知模塊
隨著智能手機的發展,現在基本上人手一臺智能手機,如何讓校園助手更好地服務學生,這是我們設計校園助手的目的。高校學生管理中,經常需要對重要的事情發出通知,一般通知都是按照一級一級傳達的,最后傳達到每個學生需要一段時間。校園助手針對這樣的需求,設計出自己的消息通知模塊,當有重要事情要通知相關的學生的時候,校園助手Android客戶端能接收到相關消息,并且及時在學生手機端提醒,就像手機來電一樣,非常方便學生的管理。
2.1 Android Notification通知結構
Notification有震動、鈴聲、文字等多種表現形式,一般情況下默認顯示大圖標、標題、內容、時間、小圖標等信息,如圖1。

圖1 文本Notification內容
2.2 創建一條消息欄Notification通知
Android創建一條消息通知,首先需要獲取一個全局的通知欄管理類NotificationManager[1]對象,notifyManager= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
然后通過NotificationCompat.Builder類對象創建具體Notification對象,設置Notification通知對象的LargeIcon大圖標、Title標題、Text內容、When時間、Number數字、SmallI-con小圖標,這些通知設置好之后,調用Builder對象的build方法,創建一個Notification對象。
NotificationCompat.Builder builder=new Notification-Compat.Builder(this);
builder.setContentTitle("通知標題");
builder.setContentText("通知內容");
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對象之后,通過NotificationManager對象notifyManager發出一條Notification通知,這樣消息欄就能顯示出一條通知消息。
notifyManager.notify(notificationID,notification);
Service[2]服務組件是Android的四大組件之一,是在后臺執行的程序,其用于長時間在后臺運行的邏輯代碼。校園助手項目消息通知模塊,需要在后臺不斷地訪問網絡,檢查服務端是否有相關的通知消息,這些功能代碼都需要在后臺完成,并且需要一直在后臺運行,所以消息通知模塊采用Service組件實現。

圖2 Service生命周期
一個Android Service服務生命周期如圖2所示,服務可以在Activity界面層調用startService啟動一個服務,啟動服務后,自動執行Oncreate方法,服務在后臺運行,直到Activity界面層調用了stopService方法,服務停止。校園手機助手啟動服務后,在Oncreate方法中啟動線程,啟動的線程中不斷訪問網絡,查詢是否有通知消息,如果有通知消息就發出一條通知顯示在手機的通知欄。
Android中訪問網絡需要開啟子線程來完成,如果每訪問一次網絡就開啟一個線程,這需要Android操作系統不斷地獲取底層資源,從而影響其它App軟件的使用效率和速度。在校園助手項目中,其查詢服務端通知消息數據是通過開啟一個子線程,然后復用這個子線程來進行,每訪問一次網絡,休眠一段時間再訪問網絡,對訪問網絡獲取的數據進行異步處理,這樣就避免了不斷開啟新的線程訪問網絡,節約了資源,提高了系統效率。

圖3 線程復用
如圖3,服務端啟動線程后,先訪問服務端數據,如果有數據就異步發出消息,發出一條Notification顯示在Android通知欄上,否則進入睡眠延時,等待下一個循環。下面是相關的線程run函數代碼實現:
public void run(){
do{
String notifResult="";
notifResult=getNotificationData();//請求服務端數據
if(!notifResult.equals("")){
if(handler!=null){
Message msg=Message.obtain();
msg.what=100;
msg.obj=notifResult;
handler.sendMessage(msg);//異步消息,請求發出Notification
}
}
try{
Thread.sleep(mLoopTime);//線程休眠
}catch(InterruptedException e){
e.printStackTrace();
}
}while(!bLoop);
}
校園助手項目通知模塊通過在Android Service組件中,利用Thread線程復用技術,訪問網絡查詢服務端通知消息數據,通過Handler異步消息機制,異步處理消息數據,在Android手機通知欄中發出通知。本文搭建了Android通知消息模塊的框架,還有不少需要完善的地方,但是有了這樣消息通知框架,對設計類似的消息通知模塊有重要的參考價值。
[1]http://developer.android.com/design/patterns/notifications.html
[2]傳智播客高教產品研發部.Android移動應用基礎教程[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-),男,江西寧都人,碩士,研究方向為移動應用開發、軟件技術。
2015年度廣東大學生科技創新培育專項,2016年度廣東職業技術學院教學改革項目,項目編號:XJJG 2016 08。