




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】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年PP胶水项目规划申请报告模板
- 2025年电机及电动操项目申请报告
- 做框架 教学课件
- 怎样使用课件教学
- 文库发布:护理伦理
- 美学欣赏教学课件
- 敬畏生命主题班会课件
- 认识整点的教学课件
- 散光护理措施
- 医院教学类培训课件
- 农村农资采购与供应长期合作协议
- 反假币培训课件
- 2025届辽宁省大连市高新区英语七年级第二学期期末学业质量监测模拟试题含答案
- 中山大学强基校测面试题
- 爱回收培训课件
- 2025年湖南省中考化学真题(解析版)
- aopa无人机培训管理制度
- 对患者的健康教育制度
- 2025至2030年中国工业控制软件行业市场运行态势及前景战略研判报告
- 中国PSRAM行业市场供需态势及发展前景研判报告
- 2025呼伦贝尔农垦集团有限公司校园招聘44人笔试参考题库附带答案详解析集合
评论
0/150
提交评论