Android Service組件在新進程綁定(bindService)過程

來源:互聯網
上載者:User

標籤:android   des   style   blog   http   java   

1、首先看兩個例子

   (1)進程內

     Client端

public class CounterService extends Service implements ICounterService {  ......public class CounterBinder extends Binder {  public CounterService getService() {  return CounterService.this;  }  }  ......}


     Server端

public class MainActivity extends Activity implements OnClickListener {  ......private ServiceConnection serviceConnection = new ServiceConnection() {  public void onServiceConnected(ComponentName className, IBinder service) {  counterService = ((CounterService.CounterBinder)service).getService();  Log.i(LOG_TAG, "Counter Service Connected");  }  ......};  ......}


(1)進程間

     Client端

public class RemoteService extends Service {private final static String TAG = "RemoteService";@Overridepublic IBinder onBind(Intent intent) {Log.i(TAG, "執行了OnBind");return new MyBinder();}private class MyBinder extends RemoteWebPage.Stub{@Overridepublic String getCurrentPageUrl() throws RemoteException{return "http://www.cnblogs.com/hibraincol/";}}}
     

     Server端

private class MyServiceConnection implements ServiceConnection{@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {Log.i(TAG, "建立串連...");remoteWebPage = RemoteWebPage.Stub.asInterface(service);try {Log.d(TAG, remoteWebPage.getCurrentPageUrl());} catch (RemoteException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Overridepublic void onServiceDisconnected(ComponentName name) {Log.i(TAG, "onServiceDisconnected...");}}

     為什麼一個是

counterService = ((CounterService.CounterBinder)service).getService();  
     另一個是
remoteWebPage = RemoteWebPage.Stub.asInterface(service);
     原因在於第一個service是CounterService對象,第二個Service是BinderProxy對象,指向MyBinder對象。

      

      為什麼進程內和進程間會有差別呢?請看:


       就在於第6步,要傳遞的參數是BinderProxy對象。

       進程內會執行如下代碼:

case BINDER_TYPE_HANDLE:               case BINDER_TYPE_WEAK_HANDLE: {                struct binder_ref *ref = binder_get_ref(proc, fp->handle);        ...........         if (ref->node->proc == target_proc) {//相同進程                     if (fp->type == BINDER_TYPE_HANDLE)                         fp->type = BINDER_TYPE_BINDER;                 else                                                   fp->type = BINDER_TYPE_WEAK_BINDER;                 fp->binder = ref->node->ptr;                   fp->cookie = ref->node->cookie;                binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL);                if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)                        printk(KERN_INFO "        ref %d desc %d -> node %d u%p\n",                               ref->debug_id, ref->desc, ref->node->debug_id, ref->node->ptr);        } else {                                               ......        }     }
       為轉換成CounterService對象做準備。
       

       進程間會執行如下代碼:

case BINDER_TYPE_HANDLE:               case BINDER_TYPE_WEAK_HANDLE: {                struct binder_ref *ref = binder_get_ref(proc, fp->handle);        .........        if (ref->node->proc == target_proc) {                         .....        } else {//不同進程                                          struct binder_ref *new_ref;                    new_ref = binder_get_ref_for_node(target_proc, ref->node);                if (new_ref == NULL) {                                         return_error = BR_FAILED_REPLY;                        goto err_binder_get_ref_for_node_failed;                }                                              fp->handle = new_ref->desc;                    binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL);                if (binder_debug_mask & BINDER_DEBUG_TRANSACTION)                        printk(KERN_INFO "        ref %d desc %d -> ref %d desc %d (node %d)\n",                               ref->debug_id, ref->desc, new_ref->debug_id, new_ref->desc, ref->node->debug_id);        }     }
        為轉換成BinderProxy對象做準備。此時BinderProxy對象是Counter進程的,MyBinder對象是CounterService進程的。它們通過處理序間通訊的方式傳遞資料。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.