3

Android 바인딩 서비스에 대한 의문점이 있습니다. 가이드 :bind service에 대한 의문점

The `bindService()` method returns immediately without a value 

을하지만이 방법의 서명이 반환 된 부울 값은 다음과 같이 설명

public abstract boolean bindService (Intent service, ServiceConnection conn, int flags) 

입니다 here 때문에 올바른 것 같다하지 않습니다 http://developer.android.com/guide/components/bound-services.html 은 약 bindService() 말한다 아래 :

If you have successfully bound to the service, true is returned; false is returned if the connection is not made so you will not receive the service object. 

그래서 질문은 다음과 같습니다. returns immediately without a value? javadoc에가 bindService()는 또한 서비스에 대한 경계가 실패하면 false를 반환 할 수 있다고하기 때문에,

void doBindService() { 
    bindService(new Intent(Binding.this, 
      LocalService.class), mConnection, Context.BIND_AUTO_CREATE); 
    mIsBound = true; 
} 

나는 mIsBound = true의 의미를 이해하지 않습니다 또한, here는, 바인드는이 방법으로 이루어집니다. 따라서 :

void doBindService() { 
    mIsBound = bindService(new Intent(Binding.this, 
      LocalService.class), mConnection, Context.BIND_AUTO_CREATE); 
} 

내가 틀렸어?

+0

서비스가 이미 실행되고 있지 않은 경우, 반환 값은 예를 들어, 이럴 거의 의미를 가지고 있으며, 당신은 반환 값이 true == 0 플래그와 결합 , 왜? 나는 모른다 ... – pskink

+0

예, 그것은 모순이다. 연결이 생성되었는지 아닌지를 알 수 있기 때문에, onServiceConnect()가 호출되었을 때만 알 수 있고 즉시 그렇지 않다. – GVillani82

+0

악화 : bindService()가 true를 반환하지만 onServiceConnected()가 호출되지 않습니다 (서비스가 시작/생성되지 않고 flags == 0 인 경우). 반환 된 값을 잊어 버리고 ServiceConnection에만 의존한다고 생각합니다 – pskink

답변

6

설명서가 잘못되었습니다. 반환 된 부울 값이 거짓 인 경우 이것은 연결 시도를 더 이상 시도하지 않음을 의미합니다. true가 반환되면 시스템이 연결을 시도하고 성공 또는 실패 할 수 있음을 의미합니다.

이 질문에 대한 답변 : "in what case does bindservice return false"을보십시오. 기본적으로 bindservice는 바인딩하려고 시도하는 서비스를 찾지 못하면 false를 반환합니다.

0

좋아요, 마침내 안드로이드에서 바인딩 서비스의 모든 뉘앙스를 배우면서 완성되었습니다. ServiceBindHelper 클래스는 "궁극적 인 진실"(내 무형 작용을 용인 할 수 있음)으로 간주 될 수 있습니다.

https://gist.github.com/attacco/987c55556a2275f62a16

사용 예 :

class MyActivity extends Activity { 
    private ServiceBindHelper<MyService> myServiceHelper; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     myServiceHelper = new ServiceBindHelper<MyService>(this) { 
      @Override 
      protected Intent createBindIntent() { 
       return new Intent(MyActivity.this, MyService.class); 
      } 

      @Override 
      protected MyService onServiceConnected(ComponentName name, IBinder service) { 
       // assume, that MyService is just a simple local service 
       return (MyService) service; 
      } 
     }; 
     myServiceHelper.bind(); 
    } 

    protected void onDestroy() { 
     super.onDestroy(); 
     myServiceHelper.unbind(); 
    } 

    protected void onStart() { 
     super.onStart(); 
     if (myServiceHelper.getService() != null) { 
      myServiceHelper.getService().doSmth(); 
     } 
    } 
}