當啟動一個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對象。