Android: java.lang.IllegalArgumentException: Service not registered

Johnny picture Johnny · Apr 14, 2017 · Viewed 23.2k times · Source

I have activity, that starts service and binds it. But I want my service to run even if the activity is closed, so I need to unbind it. When i am trying to do so (calling my function disconnectFromService), i always get this exception.

Service is successfuly bound and I can communicate with it. I am trying to unbind the service only if is bound.

I think, I try to bind and unbind the service from the same context - this - calling bind and unbind are from MainActivity functions.

Main Activity:

public class MainActivity extends AppCompatActivity {

private boolean bounded;
private SturkoPlayerService sturkoService;

...

//Connection
...
private ServiceConnection connection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        SturkoPlayerService.LocalBinder mBinder = (SturkoPlayerService.LocalBinder) service;
        sturkoService = mBinder.getService();
        bound = true;
        ...
        Log.d(DEBUG_S, "Service bind: " + (bounded && sturkoService!=null)); //Binding successful           
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        bound = false;
        sturkoService = null;
    }
};
...


//Binding in MainActivity code
...
Intent startingIntent = new Intent(this, SturkoPlayerService.class);
startingIntent.setAction(SturkoPlayerService.PLAYER_START);
bindService(startingIntent, connection, Context.BIND_AUTO_CREATE);
...

//Unbinding
private void disconnectFromService() {
    if (sturkoService != null && bound) {
        ...
        this.unbindService(connection);
    }
}
...
}

Service:

public class SturkoPlayerService extends Service {
...
    public class LocalBinder extends Binder {
        public SturkoPlayerService getService() {
            return SturkoPlayerService.this;
        }
    }
...
}

EDIT1: Logcat

04-14 11:57:51.873 2890-2890 D/sturkoPlayer: START
04-14 11:57:51.873 2890-2890 D/sturkoPlayer: Service bind: true
04-14 11:58:01.406 2890-2890 W/System.err: java.lang.IllegalArgumentException: Service not registered: ....MainActivity$1@4221e2d8
04-14 11:58:01.521 2890-2890 W/System.err:     at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:947)
04-14 11:58:01.521 2890-2890 W/System.err:     at android.app.ContextImpl.unbindService(ContextImpl.java:1277)
04-14 11:58:01.522 2890-2890 W/System.err:     at android.content.ContextWrapper.unbindService(ContextWrapper.java:405)
04-14 11:58:01.522 2890-2890 W/System.err:     at ...MainActivity.disconnectFromService(MainActivity.java:122)
04-14 11:58:01.522 2890-2890 W/System.err:     at ...MainActivity.onStop(MainActivity.java:132)
04-14 11:58:01.522 2890-2890 W/System.err:     at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1210)
04-14 11:58:01.522 2890-2890 W/System.err:     at android.app.Activity.performStop(Activity.java:5158)
04-14 11:58:01.523 2890-2890 W/System.err:     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3397)
04-14 11:58:01.523 2890-2890 W/System.err:     at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3456)
04-14 11:58:01.525 2890-2890 W/System.err:     at android.app.ActivityThread.access$1200(ActivityThread.java:149)
04-14 11:58:01.526 2890-2890 W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1353)
04-14 11:58:01.527 2890-2890 W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 11:58:01.527 2890-2890 W/System.err:     at android.os.Looper.loop(Looper.java:153)
04-14 11:58:01.527 2890-2890 W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:4987)
04-14 11:58:01.528 2890-2890 W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
04-14 11:58:01.528 2890-2890 W/System.err:     at java.lang.reflect.Method.invoke(Method.java:511)
04-14 11:58:01.529 2890-2890 W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
04-14 11:58:01.531 2890-2890 W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
04-14 11:58:01.541 2890-2890 W/System.err:     at dalvik.system.NativeStart.main(Native Method)

Edit2: in manifest, under activity

<service
    android:name=".Service.SturkoPlayerService"
    android:enabled="true"
    android:exported="true"></service>

SOLUTION:

Problem was, that "disconnectFromService()" function was called from finish() and from onStop() too. So it was called twice, when activity was about to close.

After removing call from onStop(), the exception is thrown no more.

Unfortunately this bug was just my stupidity.

Answer

Johnny picture Johnny · Apr 14, 2017

Solution

Problem was, that disconnectFromService() function was called from finish() and from onStop() too. So it was called twice, when activity was about to close.

After removing call from onStop(), the exception is thrown no more.

Unfortunately this bug was just my stupidity.