<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                當啟動一個Service時,他默認都是運行在主線程的,如果Service將要運行非常耗時或者可能被阻塞的操作時,應用程序將會被掛起,甚至會出現ANR錯誤。為了避免這一問題,應該在Service中重新啟動一個新的線程來進行這些操作。但有一個更好的方法那就是用IntentService IntentService使用隊列的方式將請求的Intent加入隊列,然后開啟一個工作線程來處理隊列中的Intent,對于異步的startService請求,IntentService會處理完成一個之后再處理第二個,每一個請求都會在一個單獨的worker thread中處理,不會阻塞應用程序的主線程,這里就給我們提供了一個思路,如果有耗時的操作與其在Service里面開啟新線程還不如使用IntentService來處理耗時操作: **service.java:** ~~~ package com.morgen.service; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class MyService extends Service { @Override public void onCreate() { super.onCreate(); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); //Service里面是不能進行耗時的操作的,必須要手動開啟一個工作線程來處理耗時操作 System.out.println("onStart"); try { Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("睡眠結束"); } @Override public IBinder onBind(Intent intent) { return null; } } ~~~ **Intentservice.java:** ~~~ package com.morgen.service; import android.app.IntentService; import android.content.Intent; public class MyIntentService extends IntentService { public MyIntentService() { super("m"); } @Override protected void onHandleIntent(Intent intent) { // IntentService里面是可以進行耗時的操作的 System.out.println("onStart"); try { Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("睡眠結束"); } } ~~~ **入口代碼:** ~~~ package com.morgen.service; import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class ServiceDemoActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startService(new Intent(this,MyService.class));//主界面阻塞,會出現Application not responding //連續兩次啟動IntentService,會發現應用程序不會阻塞,第二次的請求會再第一個請求結束之后運行 startService(new Intent(this,MyIntentService.class)); startService(new Intent(this,MyIntentService.class)); } } ~~~ **來看看IntenService的源碼(android 4.0):** ~~~ 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); } @Override public void handleMessage(Message msg) { onHandleIntent((Intent)msg.obj); stopSelf(msg.arg1); } } ~~~ 可以看出IntentService不僅有服務的功能,還有處理和循環消息的功能. **下面是onCreate()的源碼:** ~~~ public void onCreate() { super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.start(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); } ~~~ IntentService創建時就會創建Handler線程并且啟動,然后再得到當前線程的Looper對象來初始化IntentService的ServiceLooper,接著創建Servicehandler對象. **下面是onStart()的源碼:** ~~~ public void onStart(Intent intent, int startId) { Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg); } ~~~ 當啟動IntentService的時候,就會產生一條附帶startId和Intent的Message并發送到MessageQueue中,接下來Looper發現MessageQueue中有Message的時候,就會停止Handler處理消息,接下來處理的代碼如下: ~~~ public void handleMessage(Message msg) { onHandleIntent((Intent)msg.obj); stopSelf(msg.arg1); } } ~~~ 調用onHandleIntent((Intent)msg.obj),可以在這個方法里面處理我們的工作.當任務完成時就會調用stopSelf(msg.arg1)這個方法來結束指定的工作 **當所有的工作執行完后:就會執行onDestroy方法:** ~~~ public void onDestroy() { mServiceLooper.quit(); } ~~~ 從源碼中我們可以看到,IntentService是一個基于消息的服務,每次啟動該服務并不是馬上處理你的工作,而是首先會創建對應的Looper和Handler并且在MessageQueue中添加的附帶客戶Intent的Message對象,當Looper發現有Message的時候接著得到Intent對象通過在onHandleIntent((Intent)msg.obj)中調用你的處理程序.處理完后即會停止自己的服務.接著處理MessageQueue的下一個Message對象。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看