日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

IntentService解析

發(fā)布時(shí)間:2024/7/5 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IntentService解析 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

IntentService是一個(gè)專門用來(lái)處理異步線程的一個(gè)服務(wù),它內(nèi)部創(chuàng)建了一個(gè)消息隊(duì)列以及一個(gè)Handler對(duì)象,其它組件將Intent發(fā)送過(guò)來(lái)之后,IntentService會(huì)將這個(gè)Intent通過(guò)消息隊(duì)列發(fā)送到工作線程,所以,我們可以放心大膽的在IntentService內(nèi)部做耗時(shí)操作,而不必單獨(dú)開啟線程。

好,大概描述了下,我們看一下它的實(shí)現(xiàn)方式:

package android.app;import android.annotation.WorkerThread; import android.content.Intent; import android.os.Handler; import android.os.HandlerThread; import android.os.IBinder; import android.os.Looper; import android.os.Message;... public abstract class IntentService extends Service {private volatile Looper mServiceLooper;private volatile ServiceHandler mServiceHandler;private String mName;private boolean mRedelivery;private final class ServiceHandler extends Handler {public ServiceHandler(Looper looper) {super(looper);}@Overridepublic void handleMessage(Message msg) {onHandleIntent((Intent)msg.obj);stopSelf(msg.arg1);}}...public IntentService(String name) {super();mName = name;}...public void setIntentRedelivery(boolean enabled) {mRedelivery = enabled;}@Overridepublic void onCreate() {super.onCreate();HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");thread.start();mServiceLooper = thread.getLooper();mServiceHandler = new ServiceHandler(mServiceLooper);}@Overridepublic void onStart(Intent intent, int startId) {Message msg = mServiceHandler.obtainMessage();msg.arg1 = startId;msg.obj = intent;mServiceHandler.sendMessage(msg);}...@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {onStart(intent, startId);return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;}@Overridepublic void onDestroy() {mServiceLooper.quit();}...@Overridepublic IBinder onBind(Intent intent) {return null;}...@WorkerThreadprotected abstract void onHandleIntent(Intent intent); }
將注釋代碼刪除掉之后,真正的實(shí)現(xiàn)代碼很少,現(xiàn)在將對(duì)這些代碼一一說(shuō)明,首先是onCreate方法:

onCreate方法主要做了以下事情:

創(chuàng)建HandlerThread線程并啟動(dòng),使用HandlerThread對(duì)象所創(chuàng)建的Looper對(duì)象初始化ServiceHandler對(duì)象,這樣,就可以通過(guò)ServiceHandler對(duì)象HandlerThread中的消息隊(duì)列發(fā)送數(shù)據(jù)了,我們看看他們之間是如何實(shí)現(xiàn)的,首先是HandlerThread:

... package android.os;... public class HandlerThread extends Thread {int mPriority;int mTid = -1;Looper mLooper;public HandlerThread(String name) {super(name);mPriority = Process.THREAD_PRIORITY_DEFAULT;}...public HandlerThread(String name, int priority) {super(name);mPriority = priority;}...protected void onLooperPrepared() {}@Overridepublic void run() {mTid = Process.myTid();Looper.prepare();synchronized (this) {mLooper = Looper.myLooper();notifyAll();}Process.setThreadPriority(mPriority);onLooperPrepared();Looper.loop();mTid = -1;}...public Looper getLooper() {if (!isAlive()) {return null;}// If the thread has been started, wait until the looper has been created.synchronized (this) {while (isAlive() && mLooper == null) {try {wait();} catch (InterruptedException e) {}}}return mLooper;}...public boolean quit() {Looper looper = getLooper();if (looper != null) {looper.quit();return true;}return false;}...public boolean quitSafely() {Looper looper = getLooper();if (looper != null) {looper.quitSafely();return true;}return false;}...public int getThreadId() {return mTid;} }
這個(gè)中的代碼也不多,我們看主要的run方法:

@Overridepublic void run() {mTid = Process.myTid();Looper.prepare();synchronized (this) {mLooper = Looper.myLooper();notifyAll();}Process.setThreadPriority(mPriority);onLooperPrepared();Looper.loop();mTid = -1;}
run方法主要做了以下事情:

調(diào)用Looper.prepare();在當(dāng)前線程中初始化一個(gè)Looper消息循環(huán)對(duì)象,并初始化了一個(gè)消息隊(duì)列,拿到這個(gè)線程的Looper對(duì)象,然后開啟消息循環(huán)訪問(wèn)模式。

接著回到IntentService的onCreate方法中,使用剛才的工作線程中的Looper對(duì)象來(lái)初始化ServiceHandler,使mServiceHandler對(duì)象向這個(gè)線程的Looper中發(fā)送消息。


準(zhǔn)備工作做好之后,接下來(lái)的邏輯就簡(jiǎn)單了,其它組件通過(guò)startService方法,將Intent傳遞到這個(gè)服務(wù)中,會(huì)調(diào)用onStartCommand方法,onStartCommand調(diào)用onStart方法,而onStart方法會(huì)將接收到的Intent對(duì)象作為被傳送的消息實(shí)體通過(guò)ServiceHandler發(fā)送到工作線程,然后我們?nèi)绻褂玫脑?#xff0c;直接重寫onHandleIntent就可以,onHandleIntent收到對(duì)象的時(shí)候已經(jīng)處在工作線程當(dāng)中。

我們?cè)趏nHandleIntent方法中處理完畢任務(wù)之后,不必手動(dòng)去調(diào)用stopSelf去停止服務(wù),IntentService已經(jīng)幫我們做了這樣的處理。說(shuō)到這里,可能你會(huì)有疑問(wèn),如果我在很短的時(shí)間內(nèi)發(fā)送了多個(gè)請(qǐng)求,那么第一個(gè)請(qǐng)求處理完畢那服務(wù)不就終止了嗎,后面的怎么處理呢,對(duì)于這個(gè)問(wèn)題,需要詳細(xì)了解一下stopSelf的說(shuō)明:

如果我們現(xiàn)在工作在默認(rèn)模式,同一時(shí)間只有一個(gè)Intent會(huì)被處理,如果在當(dāng)前這個(gè)任務(wù)處理結(jié)束之前還有一個(gè)Intent請(qǐng)求過(guò)來(lái)的話,那它就不會(huì)被終止,它的原因與stopSelf有關(guān)系,stopSelf會(huì)判斷終止的startId是否是最后發(fā)送過(guò)來(lái)的startId,所以,疑問(wèn)解決了。


到這里基本的解釋就說(shuō)完了,有疑問(wèn)歡迎留言。

總結(jié)

以上是生活随笔為你收集整理的IntentService解析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。