Android ApiDemos示例解析(44):App->Service->Service Start Arguments Controller

本例是ApiDemos關於Service的最後一個例子,這個例子的主要目的是介紹如何向Service傳遞參數。前面的例子忽略瞭一個重要的問題: Service 的onStartCommand 或是 onStart(2.1版本之前)是使用調用它的Android組件(通常是Activity)同一個Thread來執行的,對應Activity來說,這個Thread通常是UI Thread,前面的Service例子都是提供非常簡單的服務,對於UI 性能不會有很大影響,但如果在Service中使用瞭較費時的操作,如果網絡訪問,數據庫查詢,如果還是使用UI Thread來運行的話,就可能大大降低UI的響應性能,甚至出現ANR(Application Not Response)對話框,所以通常都是在Service新建一個線程來處理來自Client的請求。

Android.os 的Handler, HandlerThread, Loop, Message 常用於Service中,其中Handler介紹可以參見Android 系統Handler用法簡介。

Android 中每個Thread都可以有一Message Queue,但除UI Thread外,Thread缺省情況下不帶Message Queue, 要為一個Thread 創建一個Message Queue,可以參見下面代碼:

[java] 
class LooperThread extends Thread { 
 public Handler mHandler; 
 public void run() { 
 Looper.prepare(); 
 mHandler = new Handler() { 
 public void handleMessage(Message msg) { 
 // process incoming messages here  
 } 
 }; 
 Looper.loop(); 
 } 
 } 
class LooperThread extends Thread {
 public Handler mHandler;
 public void run() {
 Looper.prepare();
 mHandler = new Handler() {
 public void handleMessage(Message msg) {
 // process incoming messages here
 }
 };
 Looper.loop();
 }
 }
Looper.prepare()用來創建一個Message Queue, Looper.loop() 處理消息直到Loop停止。 在Thread在創建的Handler將會和Thread的Message Queue關聯。Handler的handleMessage用來處理消息,其類型為Message類。

HandlerThread派生於Thread,用於方便創建一個帶Looper的Thread。本例就是使用瞭HandlerThread.

[java] 
private volatile Looper mServiceLooper; 
private volatile ServiceHandler mServiceHandler; 
  
private final class ServiceHandler extends Handler { 
public ServiceHandler(Looper looper) { 
 super(looper); 

  
@Override 
public void handleMessage(Message msg) { 
 … 
 } 
  
 … 
 HandlerThread thread = new HandlerThread("ServiceStartArguments", 
 Process.THREAD_PRIORITY_BACKGROUND); 
thread.start(); 
  
mServiceLooper = thread.getLooper(); 
mServiceHandler = new ServiceHandler(mServiceLooper); 
private volatile Looper mServiceLooper;
private volatile ServiceHandler mServiceHandler;
 
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
 super(looper);
}
 
@Override
public void handleMessage(Message msg) {
 …
 }
 
 …
 HandlerThread thread = new HandlerThread("ServiceStartArguments",
 Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
 
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
ServiceStartArguments Service 使用一個新創建的Thread來處理來自Client的消息,這個Thread不是UI Thread,使用HandlerThread 創建一個帶Looper的線程,而具體的消息處理是由ServiceHandler的handleMessage來處理。

Message類定義瞭消息,可以含有對消息的描述description,類型what,缺省帶arg1,arg2, obj三個參數可以直接使用。 並提供瞭obtain()靜態函數來構造一個新的Message對象。

ServiceStartArguments.Controller 為ServiceStartArguments Service的Client。它定義瞭四個按鈕,通過Intent的Extra向Service傳遞參數:

[java] 
startService(new Intent(Controller.this, 
 ServiceStartArguments.class) 
 .putExtra("name", "One")); 
  
startService(new Intent(Controller.this, 
 ServiceStartArguments.class) 
 .putExtra("name", "Two")); 
  
startService(new Intent(Controller.this, 
 ServiceStartArguments.class) 
 .putExtra("name", "Three") 
 .putExtra("redeliver", true)); 
  
startService(new Intent(Controller.this, 
 ServiceStartArguments.class) 
 .putExtra("name", "Failure") 
 .putExtra("fail", true)); 
startService(new Intent(Controller.this,
 ServiceStartArguments.class)
 .putExtra("name", "One"));
 
startService(new Intent(Controller.this,
 ServiceStartArguments.class)
 .putExtra("name", "Two"));
 
startService(new Intent(Controller.this,
 ServiceStartArguments.class)
 .putExtra("name", "Three")
 .putExtra("redeliver", true));
 
startService(new Intent(Controller.this,
 ServiceStartArguments.class)
 .putExtra("name", "Failure")
 .putExtra("fail", true));
相當於向Service傳遞瞭name (string), redeliver(boolean) ,fail(boolean)幾個參數。

StartService會觸發Service的onStartCommand方法:

[java] 
@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
 Log.i("ServiceStartArguments", 
 "Starting #" + startId + ": " + intent.getExtras()); 
 Message msg = mServiceHandler.obtainMessage(); 
 msg.arg1 = startId; 
 msg.arg2 = flags; 
 msg.obj = intent.getExtras(); 
 mServiceHandler.sendMessage(msg); 
 Log.i("ServiceStartArguments", "Sending: " + msg); 
  
 // For the start fail button, we will simulate the process dying  
 // for some reason in onStartCommand().  
 if (intent.getBooleanExtra("fail", false)) { 
 // Don't do this if we are in a retry… the system will  
 // eventually give up if we keep crashing.  
 if ((flags&START_FLAG_RETRY) == 0) { 
 // Since the process hasn't finished handling the command,  
 // it will be restarted with the command again, regardless of  
 // whether we return START_REDELIVER_INTENT.  
 Process.killProcess(Process.myPid()); 
 } 
 } 
  
 // Normally we would consistently return one kind of result…  
 // however, here we will select between these two, so you can see  
 // how they impact the behavior.  Try killing the process while it  
 // is in the middle of executing the different commands.  
 return intent.getBooleanExtra("redeliver", false) 
 ? START_REDELIVER_INTENT : START_NOT_STICKY; 

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
 Log.i("ServiceStartArguments",
 "Starting #" + startId + ": " + intent.getExtras());
 Message msg = mServiceHandler.obtainMessage();
 msg.arg1 = startId;
 msg.arg2 = flags;
 msg.obj = intent.getExtras();
 mServiceHandler.sendMessage(msg);
 Log.i("ServiceStartArguments", "Sending: " + msg);
 
 // For the start fail button, we will simulate the process dying
 // for some reason in onStartCommand().
 if (intent.getBooleanExtra("fail", false)) {
 // Don't do this if we are in a retry… the system will
 // eventually give up if we keep crashing.
 if ((flags&START_FLAG_RETRY) == 0) {
 // Since the process hasn't finished handling the command,
 // it will be restarted with the command again, regardless of
 // whether we return START_REDELIVER_INTENT.
 Process.killProcess(Process.myPid());
 }
 }
 
 // Normally we would consistently return one kind of result…
 // however, here we will select between these two, so you can see
 // how they impact the behavior.  Try killing the process while it
 // is in the middle of executing the different commands.
 return intent.getBooleanExtra("redeliver", false)
 ? START_REDELIVER_INTENT : START_NOT_STICKY;
}
ServiceStartArguments 在 onStartCommand中根據intent, flags, startId構造一個Message (obtainMessage),然後給Handle發送這個消息sendMessage。

ServiceHandler的handleMessage 則用來具體處理每個消息,

msg.arg1 = startId; startId可以用來停止指定的服務stopSelf(msg.arg1);
msg.arg2 = flags; 為 START_FLAG_REDELIVERY或是START_FLAG_RETRY。
msg.obj = intent.getExtras(); 中定義瞭name。

handleMessage會根據這些參數執行不同的操作,具體就不一一說明瞭。

 
作者:mapdigit
 

 

 

發佈留言