0

I have running service (previously successfuly binded, started and unbinded with main activity):

Intent startingIntent = new Intent(this, SturkoPlayerService.class);
startingIntent.setAction(SturkoPlayerService.PLAYER_START);
bindService(startingIntent, connection, Context.BIND_AUTO_CREATE);

When I successfuly close mainactivity (service is unbinded and keeps running) and want to reopen mainactivity with notification intent:

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("Service", 1);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); //tried also another flags
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

and then try to bind my running service

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ...

    if (getIntent() != null && getIntent().hasExtra("Service")) {
        if (!bound) {
            bindService(new Intent(this, SturkoPlayerService.class), connection, 0);
        }
    }
}

the onServiceConnected function of my Connection variable is not called

private ServiceConnection connection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        SturkoPlayerService.LocalBinder mBinder = (SturkoPlayerService.LocalBinder) service;
        sturkoService = mBinder.getService();
        bound = true;
        sturkoService.registerClient(MainActivity.this);
    }

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

Manifest:

...
<activity android:name=".MainActivity"
    android:screenOrientation="portrait"
    android:launchMode="singleTask"> //tried other modes too
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

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

P.S.: Catlog did not show somthing

Johnny
  • 193
  • 1
  • 1
  • 8
  • "service is unbinded and keeps running" -- unless you called `startService()`, the service will not keep running. If you change the `0` in your `bindService()` call to `Context.BIND_AUTO_CREATE`, and now things work, your problem is that your service was destroyed when you originally unbound from it. – CommonsWare Apr 15 '17 at 12:32
  • no it is not, my service does keep runing (it plays sound i can hear it). When i am creating it, i use Context.BIND_AUTO_CREATE (1st block of code), I use 0 only when rebinding – Johnny Apr 15 '17 at 12:46
  • 1
    "it plays sound i can hear it" -- your service is not playing sound. Android is playing sound. Just because you hear sound does not mean that your service is running. "When i am creating it, i use Context.BIND_AUTO_CREATE (1st block of code), I use 0 only when rebinding" -- I am suggesting that you **try** replacing the `0` with `Context.BIND_AUTO_CREATE`. If it now binds, your problem is that your service was destroyed when you originally unbound from it, and that your service has bugs causing it to not stop Android from playing the sound once it is destroyed. – CommonsWare Apr 15 '17 at 12:58
  • i admit, you are right @CommonsWare, now i use startService and then bind and it seems the service is live after the activity is closed – Johnny Apr 15 '17 at 14:26

1 Answers1

0

A service will only be running if:

  • There are 1+ clients bound to it, or

  • You called startService() on it and nothing has stopped it (e.g., stopSelf(), stopService())

Otherwise, once the last bound connection is unbound, the service stops.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491