Я посмотрел другие посты по этому вопросу и не вижу, что я делаю не так.Когда я нажимаю кнопку «назад» на планшете, я получаю
java.lang.RuntimeException: Unable to destroy activity {...TestActivity}: java.lang.IllegalArgumentException: Service not registered: ...TestActivity@1035a3d1
Строка, которая вызывает ошибку, здесь unbindService()
:
@Override
protected void onDestroy( ) {
super.onDestroy( ); // does putting this line first fix the "can't destroy activity; service is not registered" crash? No.
if( serviceBound ) {
getApplicationContext().unbindService( this ); // ('this' is ServiceConnection implementation)
serviceBound = false;
} else {
if( DEBUG ) Log.d( TAG, "onDestroy() found that ThermocoupleService was not bound" );
}
onStopDisposables.clear(); // because apparently onStop() isn't always called
}
Появляется IllegalArgumentException
обратитесь к this
, который является Деятельностью, которую я пытаюсь отменить.Он реализует интерфейс ServiceConnection
.
Служба привязана к onStart()
примерно так:
bindThermoServiceIntent = new Intent( getApplicationContext(), ThermocoupleService.class );
if( !( serviceBound = bindService( bindThermoServiceIntent, /*ServiceConnection interface*/ this, Context.BIND_AUTO_CREATE ) ) )
throw new RuntimeException( TAG + ": bindService() call in onStart() failed" );
serviceComponentName = getApplicationContext().startService( bindThermoServiceIntent ); // bind to it AND start it
if( DEBUG ) Log.d( TAG, "Service running with ComponentName " + serviceComponentName.toShortString() );
Кроме этой проблемы, активность и служба работают нормально.Служба использует startForeground()
для поддержания своей работы.