a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統(tǒng) > Android > 正文

andriod如何搭建自己的輪詢框架

2019-10-21 21:30:58
字體:
供稿:網(wǎng)友

很多時候Android應用需要每間隔一段時間向服務器請求數(shù)據(jù),如果服務器數(shù)據(jù)有更新則通知界面變化。Android中最常用的紅點一般采用的就是輪詢,紅點是為了在數(shù)據(jù)有更新時及時的提醒用戶,比如朋友圈更新,當用戶的朋友圈更新時就會顯示紅點,就是通過移動端不斷的向服務器查詢朋友圈的更新狀態(tài)。

相關(guān)知識點

在實現(xiàn)輪詢框架時會主要會要到下面兩個類,會結(jié)合輪詢框架對這三個類進行講解,在應用中分析會理解更加深刻。

1、IntentService IntentService是一種特殊的Service,繼承了Service并且是一個抽象類,必須創(chuàng)建它的子類才能用。IntentService可以用于執(zhí)行后臺耗時的任務,當任務執(zhí)行后會自動停止,IntentService的優(yōu)先級比一般的線程高,比較適合執(zhí)行一些優(yōu)先級高的后臺任務。

2、PendingIntent PendingIntent是延遲的intent,主要用來在某個事件完成后執(zhí)行特定的Action。PendingIntent包含了Intent及Context,所以就算Intent所屬程序結(jié)束,PendingIntent依然有效,可以在其他程序中使用。PendingIntent一般作為參數(shù)傳給某個實例,在該實例完成某個操作后自動執(zhí)行PendingIntent上的Action,也可以通過PendingIntent的send函數(shù)手動執(zhí)行,并可以在send函數(shù)中設置OnFinished表示send成功后執(zhí)行的動作。

輪詢框架實現(xiàn)

要實現(xiàn)輪詢,可以借鑒Handler中的looper機制,如下圖,維護一個消息隊列,循環(huán)的從消息隊列中取出消息來執(zhí)行,輪詢框架可以定時的向消息隊列中加入消息,然后循環(huán)中消息隊列中取出消息執(zhí)行。

 

 
andriod,輪詢框架

 

可以自己實現(xiàn)一個Looper,但是IntentService中已經(jīng)包含了一個Looper和一個HandlerThread。因此輪詢框架中使用IntentService作為循環(huán)框架。繼承IntentService接口來實現(xiàn)處理消息訪問服務器。

PollingService 用于每次輪詢時向請求服務器接口數(shù)據(jù)。

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(Intent intent) 中根據(jù)Intent所帶有的action不同來進行訪問服務器不同的接口獲取數(shù)據(jù)。

PollingUtil 用于控制輪詢服務的開始和結(jié)束 使用PollingUtil中的startPollingService來根據(jù)action和context生成一個PendingIntent,并將PendingIntent交給PollingScheduler來處理。PollingScheduler是一個線程池控制類。

public class PollingUtil { /**  * 開始輪詢服務  */ public static void startPollingService(final Context context, String action) {   //包裝需要執(zhí)行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實現(xiàn)定時向IntentService的Looper中加入消息 PollingScheduler中生成一個單線程池,addScheduleTask中定時的執(zhí)行pendingIntent.send(),其中PendingIntent是由 PendingIntent pendingIntent = PendingIntent.getService(context, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT); 生成的,pendingIntent.send()函數(shù)會調(diào)用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(); }}

代碼分析

先給出類圖之間的關(guān)系如下:

andriod,輪詢框架

PollingService繼承了IntentService,并且在PollingUtil的startPollingService方法中通過 Intent intent = new Intent(context, PollingService.class); 和將PendingIntent 與PollingService關(guān)聯(lián)起來,并將PendingIntent加入到定時執(zhí)行的線程池中,在PollingScheduler 中使用 pendingIntent.send();

由于PendingIntent與PollingService關(guān)聯(lián),所以執(zhí)行pendingIntent.send()的時候會調(diào)用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,執(zhí)行時從mServiceLooper中取出執(zhí)行,其中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);  } }

handleMessage(Message msg)中會調(diào)用onHandleIntent((Intent)msg.obj);方法,也就是在PollingService中重寫的onHandleIntent方法。 因此我們在addScheduleTask中不斷的執(zhí)行pending.send()方法,會不斷的調(diào)用IntentService中的onStart方法中的mServiceHandler.sendMessage(msg);不斷的向消息隊列中發(fā)消息,然后在onHandleIntent處理消息。 這樣一個輪詢框架就完成了。

總結(jié)

本文的輪詢框架利用了IntentService中的handler和Looper機制來實現(xiàn)循環(huán)的處理消息,由于IntentService具有服務的特性因此特別適合后臺輪詢訪問服務器數(shù)據(jù)。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 中文字幕在线观看的电影 | 日日射天天干 | 91视频一区二区三区 | 福利久久 | 欧美aa在线观看 | a国产在线观看 | 国产三级一区二区 | 欧美日本三级 | 操人网站| 综合网视频 | 最新国产福利在线 | 欧美日韩一区二区三区在线观看 | 成人免费毛片嘿嘿连载视频 | 日本一本在线 | 亚洲成人在线视频播放 | 日韩精品免费视频 | 国产精品视频999 | 国产成人精品午夜视频免费 | 一区二区三区精品视频 | 高清国产一区 | 国产富婆一级全黄大片 | 一级在线播放 | 国产精品久久久久久久久久久久久久 | 日韩成人免费 | 99视频精品| k8久久久一区二区三区 | 激情小视频网站 | 99视频在线 | 久久久久av| 欧美一级在线视频 | 伊人免费在线观看高清版 | 欧美一级片免费看 | 国产精品国产a | 91在线播 | 国产亚洲成av人片在线观看桃 | 插插插干干干 | 毛片免费在线 | 亚洲精选久久久 | 精品在线免费观看视频 | av在线影院| 亚洲精品久久久久久久久 |