Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Service組件在新進程綁定(bindService)過程

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

編輯:關於Android編程

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";
	@Override
	public IBinder onBind(Intent intent) {
		Log.i(TAG, "執行了OnBind");
		return new MyBinder();
	}

	private class MyBinder extends RemoteWebPage.Stub{
		@Override
		public String getCurrentPageUrl() throws RemoteException{
			return "http://www.cnblogs.com/hibraincol/";
		}
	}
}

Server端

private class MyServiceConnection implements ServiceConnection{

		@Override
		public 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 block
				e.printStackTrace();
			}
			
		}

		@Override
		public 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對象。<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+ICAgICAgIL34s8zE2rvh1rTQ0Mjnz8K0+sLro7o8L3A+CjxwPjwvcD4KPHByZSBjbGFzcz0="brush:java;">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進程的。它們通過進程間通信的方式傳遞數據。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved