APP下载

基于线程复用的Android校园助手的通知模块设计

2016-03-07李海平陈荣征李海文

电脑与电信 2016年12期
关键词:服务端后台线程

李海平 陈荣征 李海文

(广东职业技术学院信息工程系,广东 佛山 528041)

基于线程复用的Android校园助手的通知模块设计

李海平 陈荣征 李海文

(广东职业技术学院信息工程系,广东 佛山 528041)

校园助手项目是基于Android系统开发的应用项目,通知消息模块是校园助手项目的重要模块。校园助手项目采用线程复用技术,访问网络,查询服务端通知消息数据,从而减少不断开启新线程的开销,提高资源的使用效率;项目中的通知在Android Service组件中进行处理,Service组件中开启线程访问网络,对网络返回的数据,通过Handler异步消息机制派发,然后在通知栏发出通知。这些技术的应用对设计类似的通知消息机制有广泛的参考价值。

Android;校园助手;线程复用;通知模块

1 引言

随着智能手机的发展,现在基本上人手一台智能手机,如何让校园助手更好地服务学生,这是我们设计校园助手的目的。高校学生管理中,经常需要对重要的事情发出通知,一般通知都是按照一级一级传达的,最后传达到每个学生需要一段时间。校园助手针对这样的需求,设计出自己的消息通知模块,当有重要事情要通知相关的学生的时候,校园助手Android客户端能接收到相关消息,并且及时在学生手机端提醒,就像手机来电一样,非常方便学生的管理。

2 Notification通知技术

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);

3 Android Service服务组件

Service[2]服务组件是Android的四大组件之一,是在后台执行的程序,其用于长时间在后台运行的逻辑代码。校园助手项目消息通知模块,需要在后台不断地访问网络,检查服务端是否有相关的通知消息,这些功能代码都需要在后台完成,并且需要一直在后台运行,所以消息通知模块采用Service组件实现。

图2 Service生命周期

一个Android Service服务生命周期如图2所示,服务可以在Activity界面层调用startService启动一个服务,启动服务后,自动执行Oncreate方法,服务在后台运行,直到Activity界面层调用了stopService方法,服务停止。校园手机助手启动服务后,在Oncreate方法中启动线程,启动的线程中不断访问网络,查询是否有通知消息,如果有通知消息就发出一条通知显示在手机的通知栏。

4 Thread线程复用访问网络、异步处理网络数据、发出通知消息

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);

}

5 结束语

校园助手项目通知模块通过在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。

猜你喜欢

服务端后台线程
基于C#线程实验探究
基于国产化环境的线程池模型研究与实现
线程池调度对服务器性能影响的研究*
Wu Fenghua:Yueju Opera Artist
新时期《移动Web服务端开发》课程教学改革的研究
后台暗恋
后台朋友
后台的风景
摸清黑客套路防范木马侵入
Java的多线程技术探讨