編輯:關於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"); } ...... }; ...... }
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進程的。它們通過進程間通信的方式傳遞數據。
在沒介紹正文之前先給大家說下poi是什麼意思。由於工作的關系,經常在文件中會看到POI這三個字母的縮寫,但是一直對POI的概念和含義沒有很詳細的去研究其背後代表的意思。今
1.寫在前面的話今天我們來學習Android中如何使用Sqlite以及性能優化。2.Android平台下數據庫相關類SQLiteOpenHelper 抽象類:通過從此類繼
簡介:Volley是Google I/O 2013上Google官方發布的一款Android平台上的網絡通信庫。以前的網絡請求,要考慮開啟線程、內存洩漏、性能等等復雜的問
眾所周知,Android系統對硬件擁有較高的要求,使用一段時間以後就會出現速度變慢、多任務處理卡頓的現象,其實通過一些小技巧,便能提升設備的運行速度,大家不