Context.bindService() 세부 정보

3185 단어 Android
요 며칠 동안 서비스 바인딩에 카드가 걸려서 인터넷을 한 바퀴 돌았지만 알아볼 수 없어서 Google 문서를 찾아볼 수밖에 없었습니다. 처음에는 거절했지만, 검색이 끝난 후에 Google 문서를 놓치지 않았습니다.BindService()에 대한 Google 공식 설명을 직접 게시합니다.
Clients can also use Context.bindService() to obtain a persistent connection to a service. This likewise creates the service if it is not already running (calling onCreate() while doing so), but does not call onStartCommand(). The client will receive the IBinder object that the service returns from its onBind(Intent) method, allowing the client to then make calls back to the service. The service will remain running as long as the connection is established (whether or not the client retains a reference on the service's IBinder). Usually the IBinder returned is for a complex interface that has been written in aidl.
원래bindService(Intent 서비스, Service Connection conn, int flags)의 매개 변수는 이렇게 왔구나. 우선 Intent는 네가 귀속하거나 통신해야 하는 서비스이고 Service Connection은 서비스 귀속을 반드시 실현해야 하는 클래스로 이 클래스에서 두 개의 편지를 실현해야 한다.1:onServiceDisconnection(), 바인딩 해제 시 호출 2:
onServiceConnected(ComponentName className, IBinder service)
귀속에 성공했을 때 호출합니다. 이 Ibinder는 서비스 클래스에서 onBind () 가 되돌아오는 결과입니다.
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();
    }
};

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;
}

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

@Override
protected void onDestroy() {
    super.onDestroy();
    doUnbindService();
}

좋은 웹페이지 즐겨찾기