0

I have an activity which I use to launch a foreground service which continues to run in the background, even when the activity is closed. I bind to this service since I want to re-use the same service when the activity is stopped and resumed.

MainActivity.java

//Service button click listener
startSvcButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        stopService(new Intent(MainActivity.this, ForegroundService.class));
        startService(new Intent(MainActivity.this, ForegroundService.class));
        bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE);
        moveTaskToBack(true);

    }
});

In the service class the service is started as follows: ForegroundService.java

startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, serviceNotification);
return Service.START_STICKY;

The problem I have is that when the MainActivity is stopped, unBind is called, which is good, but when I resume the MainActivity and call bindService again, it never happens.

The foreground service is running, I call bindService again from the onResume of the activity but mBound and mService stays null, even when I accommodate the async bindService (I know the while loop is not good but it is for debugging) The serviceConnection is ok.

MainActivity.class:

if (fsRunning) {
    if (!mBound) {
        bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE);
    }
    do {
        SystemClock.sleep(500);

    } while (!mBound);

}

How do I get the foreground service to rebind to MainActivity after on onStop of the activity and triggering a new bindService?

eben80
  • 83
  • 2
  • 7
  • show the code where `unbindService` is called - edit: and of course remove that `"do { SystemClock.sleep(500); } while (!mBound);"` - it is pointless - how `mBound` can be changed in that loop? – pskink Dec 04 '18 at 08:14
  • also whats the point in calling `stopService` before `startService`? – pskink Dec 04 '18 at 08:21
  • @pskink unbindService is not called explicitly, it happens automatically when the activity goes to onStop. The while loop is there because bindService is asynchronous and I wanted to see if i just needed to give it more time before it becomes rebound. stopService is called to make sure there are no other service instances of the same service class running when you hit the button. – eben80 Dec 04 '18 at 09:21
  • 1) any service is a singleton - you cannot start two or more instances of the same service 2) asynchronous does not mean that it is done in different thread - you have `ServiceConnection` to listen when it is bound – pskink Dec 04 '18 at 09:23
  • and no: `unbindService` is not called automatically - you have to call it explicitly - othervise you will see `"service connection has leaked something" warning on the logcat – pskink Dec 04 '18 at 09:38
  • @pskink thanks for those comments. What would cause `mBound` to remain false and `mService` to stay null even after `bindService` is being called again after `onResume` of the `MainActivity`? 'ServiceConnection` is not triggered at the second `bindService`. – eben80 Dec 04 '18 at 09:43
  • tried this [example](https://developer.android.com/guide/components/bound-services#Binder)? sorry i have no idea what `mService` you mean as there is no `mService` in the code you posted – pskink Dec 04 '18 at 09:45
  • @eben80 "What would cause `mBound` to remain false?" The framework can't create the `Service` until you _return_ from `onResume()`. You have to give control back to the message loop. Think of `bindService()` as putting a "please connect me to the service" message in the queue. That queue is processed by the same message loop that handles tap inputs, view drawing, etc., on the UI thread, so until you return control, your message loop is blocked. `onServiceConnected()` will never get called from _inside_ `bindService()`. – greeble31 Dec 04 '18 at 14:03
  • @greeble31 thank you very much for this. I moved my `bindService` call to onStart and moved the code that needed to be executed after to `onServiceConnected` this fixed the issue for me. – eben80 Dec 05 '18 at 15:39

1 Answers1

2

I moved my bindService call to onStart and moved the code that needed to be executed after to onServiceConnected this fixed the issue for me. unbindService was called from onStop in the activity.

Thanks to @greeble31 for pointing out the fault in my logic.

THIS link also has very helful information about the timing of binding

eben80
  • 83
  • 2
  • 7