【起航計劃 034】2015 起航計劃 Android APIDemo的魔鬼步伐 33 App->Service->Local Service Binding 綁定服務 ServiceConnection Binder

來源:互聯網
上載者:User

標籤:

本例和下列Local Service Controller 的Activity代碼都定義在LocalServiceActivities.Java 中,作為LocalServiceActivities 內部類實現的。 調用的Service為LocalService。

LocalService既可以做為“Started” Service,也可以做為”Bound” Service。

一個“Bound” Service 可以通過Client/Service模式提供Service。它運行應用程式組件(比如Activity)與之綁定,然後接受請求並返迴響應或者提供進 程間通訊機制,它的生命週期通常與調用它的組件(如Activity)相同。 而對於LocalService即作為“Started” Service又作為“Bound”Service,如果LocalService已作為“Started” Service啟動,中即使所有Client都斷開與它的綁定,LocalService也不會停止。

如果一個Service需要作為“Bound”Service運行其它組件與之綁定,就必須實現onBind方法。這個方法返回一個IBound對象給Client。Client可以通過這個IBind對象來調用Service的方法。

Client可以通過bindService來實現與“Bound”Service的綁定,Service 與 Client 的綁定是非同步實現的,因此Client 需要通過 ServiceConnection 介面來監視與Service之間的串連。 Client 調用bindService 時,bindService 立即返回,之後當Android系統為Client 和Service之間建立起連結後調用 ServiceConnection 的 onServiceConnection 來通知Client 與Service之間的連結建立成功,並給Client返回 Service 提供的IBind對象。

LocalService 只提供給同一個Application的組件使用,不提供處理序間通訊介面,因此只需要派生一個Binder的子類如果直接的函數調用介面。

// Class for clients to access.  Because we know // this service always runs in the same process as //its clients, we don‘t need to deal with IPC.public class LocalBinder extends Binder { LocalService getService() { return LocalService.this; }}

LocalBinder只提供了一個方法getService ,返回LocalService 對象自身。

LocalService的 onBind方法定義:

    @Override    public IBinder onBind(Intent intent) {        return mBinder;    }    // This is the object that receives interactions from clients.  See    // RemoteService for a more complete example.    private final IBinder mBinder = new LocalBinder();

 

 onBind的傳回值為mBinder 為 LocalBinder類對象。它定義了一個方法getService。

再來看看Client類 LocalServiceActivities.Binding 的實現:

        private LocalService mBoundService;                private ServiceConnection mConnection = new ServiceConnection() {            public void onServiceConnected(ComponentName className, IBinder service) {                // This is called when the connection with the service has been                // established, giving us the service object we can use to                // interact with the service.  Because we have bound to a explicit                // service that we know is running in our own process, we can                // cast its IBinder to a concrete class and directly access it.                mBoundService = ((LocalService.LocalBinder)service).getService();                                // Tell the user about this for our demo.                Toast.makeText(Binding.this, R.string.local_service_connected,                        Toast.LENGTH_SHORT).show();            }            public void onServiceDisconnected(ComponentName className) {                // This is called when the connection with the service has been                // unexpectedly disconnected -- that is, its process crashed.                // Because it is running in our same process, we should never                // see this happen.                mBoundService = null;                Toast.makeText(Binding.this, R.string.local_service_disconnected,                        Toast.LENGTH_SHORT).show();            }        };

 

mConnection定義了ServiceConnection介面,onServiceConnected ,onServiceDisconnected分別在Service與Client 之間建立連結和取消連結時調用。其中IBinder service 就是 Service 的onBind的返回對象。 這裡將其通過類型轉換為LocalService也就是mBoundService。

 

Client 通過調用bindService建立與Service之間的綁定:

        void doBindService() {            // Establish a connection with the service.  We use an explicit            // class name because we want a specific service implementation that            // we know will be running in our own process (and thus won‘t be            // supporting component replacement by other applications).            bindService(new Intent(Binding.this,                     LocalService.class), mConnection, Context.BIND_AUTO_CREATE);            mIsBound = true;        }

 

如果需要斷開與Service之間的綁定,則調用unbindService.

        void doUnbindService() {            if (mIsBound) {                // Detach our existing connection.                unbindService(mConnection);                mIsBound = false;            }        }

 

如果你熟悉Socket編程, Client 綁定”Bound”Service 的方法和使用Socket通訊非常類似。

 

【起航計劃 034】2015 起航計劃 Android APIDemo的魔鬼步伐 33 App->Service->Local Service Binding 綁定服務 ServiceConnection Binder

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.