下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】andriod搭建轮询框架的方法
在下给大家分享一下andriod搭建轮询框架的方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!很多时候Android应用需要每间隔一段时间向服务器请求数据,如果服务器数据有更新则通知界面变化。Android中最常用的红点一般采用的就是轮询,红点是为了在数据有更新时及时的提醒用户,比如朋友圈更新,当用户的朋友圈更新时就会显示红点,就是通过移动端不断的向服务器查询朋友圈的更新状态。相关知识点在实现轮询框架时会主要会要到下面两个类,会结合轮询框架对这三个类进行讲解,在应用中分析会理解更加深刻。1、IntentServiceIntentService是一种特殊的Service,继承了Service并且是一个抽象类,必须创建它的子类才能用。IntentService可以用于执行后台耗时的任务,当任务执行后会自动停止,IntentService的优先级比一般的线程高,比较适合执行一些优先级高的后台任务。2、PendingIntentPendingIntent是延迟的intent,主要用来在某个事件完成后执行特定的Action。PendingIntent包含了Intent及Context,所以就算Intent所属程序结束,PendingIntent依然有效,可以在其他程序中使用。PendingIntent一般作为参数传给某个实例,在该实例完成某个操作后自动执行PendingIntent上的Action,也可以通过PendingIntent的send函数手动执行,并可以在send函数中设置OnFinished表示send成功后执行的动作。轮询框架实现要实现轮询,可以借鉴Handler中的looper机制,如下图,维护一个消息队列,循环的从消息队列中取出消息来执行,轮询框架可以定时的向消息队列中加入消息,然后循环中消息队列中取出消息执行。可以自己实现一个Looper,但是IntentService中已经包含了一个Looper和一个HandlerThread。因此轮询框架中使用IntentService作为循环框架。继承IntentService接口来实现处理消息访问服务器。PollingService用于每次轮询时向请求服务器接口数据。public
class
PollingService
extends
IntentService
{
public
static
final
String
ACTION_CHECK_CIRCLE_UPDATE="ACTION_CHECK_CIRCLE_UPDATE";
public
static
final
long
DEFAULT_MIN_POLLING_INTERVAL
=
60000;//最短轮询间隔1分钟
public
PollingService()
{
super("PollingService");
}
@Override
protected
void
onHandleIntent(Intent
intent)
{
if
(intent
==
null)
return;
final
String
action
=
intent.getAction();
if
(ACTION_CHECK_Circle_UPDATE.equals(action))
{
CheckCircleOfFriendsUpdate();//这个是访问服务器获取朋友圈是否更新
}
}
}PollingService用来处理接到轮询的消息之后在onHandleIntent(Intentintent)中根据Intent所带有的action不同来进行访问服务器不同的接口获取数据。PollingUtil用于控制轮询服务的开始和结束使用PollingUtil中的startPollingService来根据action和context生成一个PendingIntent,并将PendingIntent交给PollingScheduler来处理。PollingScheduler是一个线程池控制类。public
class
PollingUtil
{
/**
*
开始轮询服务
*/
public
static
void
startPollingService(final
Context
context,
String
action)
{
//包装需要执行Service的Intent
Intent
intent
=
new
Intent(context,
PollingService.class);
intent.setAction(action);
PendingIntent
pendingIntent
=
PendingIntent.getService(context,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
PollingScheduler.getInstance().addScheduleTask(pendingIntent,
0,
PollingService.DEFAULT_MIN_POLLING_INTERVAL);
}
}
/**
*
停止轮询服务
*
*
@param
context
*/
public
static
void
stopPollingServices(Context
context,
String
action)
{
PollingScheduler.getInstance().clearScheduleTasks();
}
}PollingScheduler实现定时向IntentService的Looper中加入消息PollingScheduler中生成一个单线程池,addScheduleTask中定时的执行pendingIntent.send(),其中PendingIntent是由PendingIntentpendingIntent=PendingIntent.getService(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);生成的,pendingIntent.send()函数会调用Service.startService()来开启一个服务。public
class
PollingScheduler
{
private
static
PollingScheduler
sInstance;
private
ScheduledExecutorService
mScheduler;
private
PollingScheduler()
{
mScheduler
=
Executors.newSingleThreadScheduledExecutor();
}
public
static
synchronized
PollingScheduler
getInstance()
{
if
(sInstance
==
null)
{
sInstance
=
new
PollingScheduler();
}
if
(sInstance.mScheduler.isShutdown())
{
sInstance.mScheduler
=
Executors.newSingleThreadScheduledExecutor();
}
return
sInstance;
}
public
void
addScheduleTask(final
PendingIntent
pendingIntent,
long
initialDelay,
long
period)
{
Runnable
command
=
new
Runnable()
{
@Override
public
void
run()
{
try
{
pendingIntent.send();
}
catch
(PendingIntent.CanceledException
e)
{
e.printStackTrace();
}
}
};
mScheduler.scheduleAtFixedRate(command,
initialDelay,
period,
TimeUnit.MILLISECONDS);
}
public
void
clearScheduleTasks()
{
mScheduler.shutdownNow();
}
}代码分析先给出类图之间的关系如下:
PollingService继承了IntentService,并且在PollingUtil的startPollingService方法中通过Intentintent=newIntent(context,PollingService.class);和将PendingIntent与PollingService关联起来,并将PendingIntent加入到定时执行的线程池中,在PollingScheduler中使用pendingIntent.send();由于PendingIntent与PollingService关联,所以执行pendingIntent.send()的时候会调用PollingIntentServide中的onStart()方法。onStart()方法是IntentService中的方法,代码如下:
@Override
public
void
onStart(@Nullable
Intent
intent,
int
startId)
{
Message
msg
=
mServiceHandler.obtainMessage();
msg.arg1
=
startId;
msg.obj
=
intent;
mServiceHandler.sendMessage(msg);
}在onstart()中有一个mServiceHandler.sendMessage(msg);,找到mServiceHandler的生成位置:
@Override
public
void
onCreate()
{
super.onCreate();
HandlerThread
thread
=
new
HandlerThread("IntentService["
+
mName
+
"]");
thread.start();
mServiceLooper
=
thread.getLooper();
mServiceHandler
=
new
ServiceHandler(mServiceLooper);
}在IntentService的onCreate方法中生成了一个HandlerThread,一个mServiceLooper,一个mServiceHandler,其中mServiceHandler.sendMessage(msg)中的msg都会放到mServiceLooper,执行时从mServiceLooper中取出执行,其中ServiceHandler的代码如下
private
final
class
ServiceHandler
extends
Handler
{
public
ServiceHandler(Looper
looper)
{
super(looper);
}
@Override
public
void
handleMessage(Message
msg)
{
onHandleIntent((Intent)msg.obj);
stopSelf(msg.arg1);
}
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 学校园安全隐患大排查大整治百日攻坚专项行动实施方案
- 2025年北京协和医院变态(过敏)反应科合同制科研助理招聘备考题库及完整答案详解1套
- 2025青岛卫生人才教育培训平台公需科目试题及答案
- 2025年绵阳市公安局安州区分局公开招聘警务辅助人员的备考题库及参考答案详解一套
- 广东2025年民生银行汕头分行社会招聘备考题库有答案详解
- 药明合联ADC浪潮高壁垒CDMO迎来战略机遇期首次覆盖给予“买入”评级
- java课程设计数据库
- 2025 九年级语文下册小说情节高潮分析课件
- 中共东莞市委外事工作委员会办公室2025年公开招聘编外聘用人员备考题库及参考答案详解一套
- 2025年全球锂电池铜箔行业竞争格局报告
- 外科题库选择题及答案
- 专题07 人与动物读后续写-2025年高考英语话题写作高频热点通关攻略(原卷版)
- 思政大一上期末复习测试附答案
- 乳腺癌靶向治疗药物研究进展
- 墙绘施工合同协议书
- 国家开放大学行管专科《行政组织学》期末纸质考试总题库(2025春期版)
- 中国慢性冠脉综合征患者诊断及管理指南2024版解读
- iso28000-2022供应链安全管理手册程序文件表单一整套
- 2024年保安员证考试题库及答案(共130题)
- 2024年中国红芪市场调查研究报告
- NB-T42167-2018预制舱式二次组合设备技术要求
评论
0/150
提交评论