很多時(shí)候Android應(yīng)用需要每間隔一段時(shí)間向服務(wù)器請(qǐng)求數(shù)據(jù),如果服務(wù)器數(shù)據(jù)有更新則通知界面變化。Android中最常用的紅點(diǎn)一般采用的就是輪詢,紅點(diǎn)是為了在數(shù)據(jù)有更新時(shí)及時(shí)的提醒用戶,比如朋友圈更新,當(dāng)用戶的朋友圈更新時(shí)就會(huì)顯示紅點(diǎn),就是通過(guò)移動(dòng)端不斷的向服務(wù)器查詢朋友圈的更新?tīng)顟B(tài)。
相關(guān)知識(shí)點(diǎn)
在實(shí)現(xiàn)輪詢框架時(shí)會(huì)主要會(huì)要到下面兩個(gè)類,會(huì)結(jié)合輪詢框架對(duì)這三個(gè)類進(jìn)行講解,在應(yīng)用中分析會(huì)理解更加深刻。
1、IntentService IntentService是一種特殊的Service,繼承了Service并且是一個(gè)抽象類,必須創(chuàng)建它的子類才能用。IntentService可以用于執(zhí)行后臺(tái)耗時(shí)的任務(wù),當(dāng)任務(wù)執(zhí)行后會(huì)自動(dòng)停止,IntentService的優(yōu)先級(jí)比一般的線程高,比較適合執(zhí)行一些優(yōu)先級(jí)高的后臺(tái)任務(wù)。
2、PendingIntent PendingIntent是延遲的intent,主要用來(lái)在某個(gè)事件完成后執(zhí)行特定的Action。PendingIntent包含了Intent及Context,所以就算Intent所屬程序結(jié)束,PendingIntent依然有效,可以在其他程序中使用。PendingIntent一般作為參數(shù)傳給某個(gè)實(shí)例,在該實(shí)例完成某個(gè)操作后自動(dòng)執(zhí)行PendingIntent上的Action,也可以通過(guò)PendingIntent的send函數(shù)手動(dòng)執(zhí)行,并可以在send函數(shù)中設(shè)置OnFinished表示send成功后執(zhí)行的動(dòng)作。
輪詢框架實(shí)現(xiàn)
要實(shí)現(xiàn)輪詢,可以借鑒Handler中的looper機(jī)制,如下圖,維護(hù)一個(gè)消息隊(duì)列,循環(huán)的從消息隊(duì)列中取出消息來(lái)執(zhí)行,輪詢框架可以定時(shí)的向消息隊(duì)列中加入消息,然后循環(huán)中消息隊(duì)列中取出消息執(zhí)行。
可以自己實(shí)現(xiàn)一個(gè)Looper,但是IntentService中已經(jīng)包含了一個(gè)Looper和一個(gè)HandlerThread。因此輪詢框架中使用IntentService作為循環(huán)框架。繼承IntentService接口來(lái)實(shí)現(xiàn)處理消息訪問(wèn)服務(wù)器。
PollingService 用于每次輪詢時(shí)向請(qǐng)求服務(wù)器接口數(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();//這個(gè)是訪問(wèn)服務(wù)器獲取朋友圈是否更新 } }} PollingService 用來(lái)處理接到輪詢的消息之后在 onHandleIntent(Intent intent) 中根據(jù)Intent所帶有的action不同來(lái)進(jìn)行訪問(wèn)服務(wù)器不同的接口獲取數(shù)據(jù)。
PollingUtil 用于控制輪詢服務(wù)的開始和結(jié)束 使用PollingUtil中的startPollingService來(lái)根據(jù)action和context生成一個(gè)PendingIntent,并將PendingIntent交給PollingScheduler來(lái)處理。PollingScheduler是一個(gè)線程池控制類。
public class PollingUtil { /** * 開始輪詢服務(wù) */ 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); } } /** * 停止輪詢服務(wù) * * @param context */ public static void stopPollingServices(Context context, String action) { PollingScheduler.getInstance().clearScheduleTasks(); } } PollingScheduler實(shí)現(xiàn)定時(shí)向IntentService的Looper中加入消息 PollingScheduler中生成一個(gè)單線程池,addScheduleTask中定時(shí)的執(zhí)行pendingIntent.send(),其中PendingIntent是由 PendingIntent pendingIntent = PendingIntent.getService(context, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT); 生成的,pendingIntent.send()函數(shù)會(huì)調(diào)用Service.startService()來(lái)開啟一個(gè)服務(wù)。
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)系如下:
PollingService繼承了IntentService,并且在PollingUtil的startPollingService方法中通過(guò) Intent intent = new Intent(context, PollingService.class); 和將PendingIntent 與PollingService關(guān)聯(lián)起來(lái),并將PendingIntent加入到定時(shí)執(zhí)行的線程池中,在PollingScheduler 中使用 pendingIntent.send();
由于PendingIntent與PollingService關(guān)聯(lián),所以執(zhí)行pendingIntent.send()的時(shí)候會(huì)調(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()中有一個(gè) 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方法中生成了一個(gè)HandlerThread,一個(gè)mServiceLooper,一個(gè)mServiceHandler,其中mServiceHandler.sendMessage(msg)中的msg都會(huì)放到mServiceLooper,執(zhí)行時(shí)從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)中會(huì)調(diào)用onHandleIntent((Intent)msg.obj);方法,也就是在PollingService中重寫的onHandleIntent方法。 因此我們?cè)赼ddScheduleTask中不斷的執(zhí)行pending.send()方法,會(huì)不斷的調(diào)用IntentService中的onStart方法中的mServiceHandler.sendMessage(msg);不斷的向消息隊(duì)列中發(fā)消息,然后在onHandleIntent處理消息。 這樣一個(gè)輪詢框架就完成了。
總結(jié)
本文的輪詢框架利用了IntentService中的handler和Looper機(jī)制來(lái)實(shí)現(xiàn)循環(huán)的處理消息,由于IntentService具有服務(wù)的特性因此特別適合后臺(tái)輪詢?cè)L問(wèn)服務(wù)器數(shù)據(jù)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注