나는 이것이 다른 어떤 것보다 코드에 관한 문제가 더 많기를 바라고 있으며 거기에있는 누군가가 문제를 추적하는 데 도움이되기를 바랍니다.실행중인 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;
}
}
}
내 활동이 Tab 위젯 내에서 실행 중이며이 상황에서 로컬 컨텍스트 대신 getApplicationContext()를 전달해야합니다. – yamspog
나는 bindService()가 다소 비동기 적이라고 말할 수있다. onCreate에서 호출하고 나중에 oncreate에서 사용하려고했지만 null 포인터 예외가 발생했습니다. onCreate가 끝나기 전까지는 실제로 바인딩되지 않은 것으로 보입니다. 서비스에 구속력이없는 것을 어떻게 알 수 있습니까? 실제로 서비스를 사용하는 곳에서 더 많은 코드를 게시 할 수 있습니까? – Falmarri