6

Currently, I need a bound (Music)Service, because I need to interact with it. But I also want it to not be stopped, even when all components have unbound themselves.

As the Android Developer Guide says

"[...] Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed."

The Guide also says

"[...] your service can work both ways—it can be started (to run indefinitely) and also allow binding."

In my application, the service is started when the application starts. I want to have this service destroyed only by a user-click on a close-button I am displaying in a custom notification. But currently, when I am destroying my MainActivity the service also stops.

This is where I am now, this is called when I want to create my Service:

public void createServiceConnection(){
     musicConnection = new ServiceConnection(){
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            MusicService.MusicBinder binder = (MusicService.MusicBinder)service;
            musicSrv = binder.getService();
            attachMusicService();
        }
    };
}

...which calls this:

public void attachMusicService(){
    playerFragment.setMusicService(musicSrv);
    musicSrv.attach(context); //need this for my listeners, nevermind
    bindService(context);
}

...which calls this:

    public void bindService(Context act){
        if(playIntent==null){
            playIntent = new Intent(act, MusicService.class);
            act.startService(playIntent);

            act.bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
        }else{
            act.startService(playIntent);
            act.bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
        }

//finished. I can do stuff with my Service here.
    }

Have I misunderstood something? I feel like the service should keep running, even the activity is destroyed, because I first made a started service and then bound to it.

Dominik Seemayr
  • 830
  • 2
  • 12
  • 30

2 Answers2

0

Bind to your service from custom Application class. I don't think you can keep service alive after activity that's bound to it is destroyed (when onDestroy is called). You can keep service alive if activity pauses (onPause) by calling startForeground from service

Wukash
  • 666
  • 5
  • 9
  • 1
    I've read, that when I start the service with "startService()" it follows the life-cycle of a started service. So I can bind und unbind as often I want without the service being destroyed. But getting back to your answer: From a "custom Application class"? What do you mean exactly? – Dominik Seemayr Mar 20 '16 at 17:53
  • You can extend Application class like this: public class MyApplication extends Application. And then add in your manifest application tag this: android:name=".MyApplication". Override onCreate method of your Application and bind to service from there – Wukash Mar 20 '16 at 17:55
  • sounds interesting, have you tried something similar before? But just figured out my problem and it's working now. If you are interested in this topic - I posted the answer - thank you anyway! – Dominik Seemayr Mar 20 '16 at 18:03
0

Seems like the code was correct.

According to this Question I found out that my problem was the notification I displayed, wich is pretty interesting.

Seems like that a Service that is created for running indefinitely needs to have a Notification wich is displayed by startForeground(NOTIFY_ID, notification);.

I showed my notification with notificationmanager.notify(NOTIFY_ID, notification); before, now I have

 `notificationmanager.notify(NOTIFY_ID, notification);
  startForeground(NOTIFY_ID, notification);`

and the service won't stop anymore after all my bound Activities are destroyed.

Dominik Seemayr
  • 830
  • 2
  • 12
  • 30
  • First you say 'Seems like my code was correct.`, then you prove it wasn't :) – Wukash Mar 20 '16 at 18:04
  • Sorry. Meant "..the code I posted..". The problem wasn't included in the code I showed in my Question. – Dominik Seemayr Mar 20 '16 at 18:06
  • Yeah i know, it just made me smile a bit :). I was sure you were already calling startForeground since that's the most important part - to make service background – Wukash Mar 20 '16 at 18:08