2010-07-28 6 views
1

나는 이것이 다른 어떤 것보다 코드에 관한 문제가 더 많기를 바라고 있으며 거기에있는 누군가가 문제를 추적하는 데 도움이되기를 바랍니다.실행중인 Android 서비스에 바인딩하는 방법은 무엇입니까?

startService()로 서비스를 시작하는 다른 코드가 있으며 디버거가 DecoderService의 onCreate() 함수에 도달 할 때 서비스가 시작되었는지 확인할 수 있습니다.

그러나 bindService는 실행중인 서비스에 바인드되지 않습니다. 비동기 호출입니까? 완료 할 때까지 기다려야합니까?

public class ResultsActivity extends Activity { 

protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 

    Intent decoderIntent = new Intent(this,DecoderService.class); 
    _isBound = bindService(decoderIntent,mConnection,Context.BIND_AUTO_CREATE); 
    _textView.start(_mBoundService); 
} 

private boolean _isBound = false; 
private DecoderService _mBoundService; 

private ServiceConnection mConnection = new ServiceConnection() { 
    public void onServiceConnected(ComponentName className, IBinder service) { 
     _mBoundService = ((DecoderService.LocalBinder)service).getService(); 
    } 

    public void onServiceDisconnected(ComponentName className) 
     _mBoundService = null; 
    } 
}; 
} 

    public class DecoderService extends Service { 


protected void onHandleIntent(Intent intent) { 
    // TODO Auto-generated method stub 
    _numCompleted = 5; 
    _numTotal = 100; 
} 

protected int _numCompleted = 0; 
protected int _numTotal = 0; 


public void onCreate() { 
    onHandleIntent(null); 
} 


@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(); 

public class LocalBinder extends Binder { 
    DecoderService _ref = null; 
    public LocalBinder() 
    { 
     _ref = DecoderService.this; 
    } 
    DecoderService getService() { 
     return DecoderService.this; 
    } 

} 
} 
+0

내 활동이 Tab 위젯 내에서 실행 중이며이 상황에서 로컬 컨텍스트 대신 getApplicationContext()를 전달해야합니다. – yamspog

+0

나는 bindService()가 다소 비동기 적이라고 말할 수있다. onCreate에서 호출하고 나중에 oncreate에서 사용하려고했지만 null 포인터 예외가 발생했습니다. onCreate가 끝나기 전까지는 실제로 바인딩되지 않은 것으로 보입니다. 서비스에 구속력이없는 것을 어떻게 알 수 있습니까? 실제로 서비스를 사용하는 곳에서 더 많은 코드를 게시 할 수 있습니까? – Falmarri

답변

4

이 비동기 호출 내가 완전한 에 뭔가를 기다릴 필요가 ?

개체에 onServiceConnected()이 호출 될 때까지 바인딩이 준비되지 않았습니다.

+1

이 문제의 원인은 무엇입니까? 이것이'onResume() '이전에 일어난 것을 확인하는 방법이 있습니까? – finiteloop

+0

@commonsware 장기간 실행되는 서비스를 바인딩 한 다음 바인딩을 해제하고 서비스를 계속 실행하면 서비스가 가능하거나 좋은 성능을 발휘할 수 있습니다. – AlexSanchez

관련 문제