android - Service keeps getting destroyed -


i have service uses media player play music. service started activity , if playing , user moves away activity service should keep playing in background. running service in foreground seems work (i can see notification), in close cases service destroyed (ondestroy called system on service). know using startforeground not mean service never killed, keeps getting destroyed right away, guess few ressources forcing system kill it, not reason that.

this how implemented it: in oncreate of activity, start (in background) , bind service. in onpause bring service foreground not destroyed well:

protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_play);      // start service     startservice(new intent(this, myservice.class));     // connect service     bindtoservice();     ... }  @override protected void ondestroy() {     unbindfromservice();     super.ondestroy(); };  @override protected void onstart() {     super.onstart(); }  @override protected void onstop() {     super.onstop(); }  @override protected void onpause() {     super.onpause();      if (mediaplayerservice.getinstance().getstatus() == media_player_status.started) {         // current playing => keep service running         mservice.startforeground();     } else {         // stop service         stopservice(new intent(mainactivity.this, myservice.class));     } }  @override protected void onresume() {     super.onresume();      // remove service foreground     if (mservice != null) {         mservice.stopforeground();     } }  void bindtoservice() {     // establish connection service. use explicit     // class name because want specific service implementation     // know running in our own process (and won't     // supporting component replacement other applications).     bindservice(new intent(mainactivity.this, myservice.class), mconnection, context.bind_auto_create);     misbound = true; }  void unbindfromservice() {     if (misbound) {         // detach our existing connection.         unbindservice(mconnection);         misbound = false;     } }  private final serviceconnection mconnection = new serviceconnection() {     @override     public void onserviceconnected(componentname classname, ibinder service) {         // called when connection service has been         // established, giving service object can use         // interact service. because have bound explicit         // service know running in our own process, can         // cast ibinder concrete class , directly access it.         mservice = ((myservice.localbinder) service).getservice();         mservice.registerclient(mainactivity.this);     }      @override     public void onservicedisconnected(componentname classname) {         // called when connection service has been         // unexpectedly disconnected -- is, process crashed.         // because running in our same process, should never         // see happen.         mservice = null;         mservice.unregisterclient(mainactivity.this);     } }; 

and start/stopforeground functions in service this:

public void startforeground() {     string songname = "blabla";     // assign song name songname     pendingintent pi = pendingintent.getactivity(getapplicationcontext(), 0,             new intent(getapplicationcontext(), mainactivity.class),             pendingintent.flag_update_current);     notification notification = new notification();     notification.tickertext = songname;     notification.icon = r.drawable.ic_launcher;     notification.flags |= notification.flag_ongoing_event;     notification.setlatesteventinfo(getapplicationcontext(), "musicplayersample",             "playing: " + songname, pi);     startforeground(notification_id, notification); }  public void stopforeground() {     stopforeground(true); } 

any ideas why service keeps getting destroyed if move away activity?

here's answer.

a bound service runs long application component bound it. multiple components can bind service @ once, when of them unbind, service destroyed.

you should call startservice() instead.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -