最終更新:2016-01-19 (火) 14:34:01 (3011d)  

Service.onBind
Top / Service.onBind

Return the communication channel to the service.

別のコンポーネントがContext.bindService() を呼び出してサービスをバインドしたいときに、システムにより呼び出される

abstract IBinder onBind(Intent intent)
  • クライアントがサービスと通信するために使用する、インターフェイスIBinderを実装したクラスを返す必要がある
  • このメソッドの実装は必須だが、バインドを許可したくなければ、null を返すようにしておく
  • 呼び出し元で呼ばれるのはServiceConnection.onServiceConnected(ComponentName name, IBinder service)

メモ

  • このメソッドの実装は常に必要ですが、バインドを許可しない場合は、null を返す必要があります。

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
        return new 
    }

関連

順序

戻り値のIBinder

  • android.os.IBinderインターフェイスを実装したandroid.os.Binderを継承したクラスを返す
  • 以下のいずれかのを行う Binder のインスタンスを作成します。
    • クライアントが呼び出せる public メソッドを含める
    • クライアントが呼び出せる public メソッドを持つ、現在の Service インスタンスを返す
    • または、クライアントが呼び出せる public メソッドを持つサービスによりホストされた別のクラスのインスタンスを返す
    public class MyIntentService extends IntentService {
      final IBinder binder = new MyBinder();
      ...
    
      public class MyBinder extends Binder {
        MyIntentService getService(){
          return MyIntentService.this;
        }    
      }
      public IBinder onBind(Intent intent) {
        return binder;
      }
      ...
    }
  • 呼び出し元
    myService = ((MyIntentService.MyBinder)service).getService();

参考

関連