android - Service dies when activity dies -


hope can me, because sure can done... don't seem able it.

i'm doing task can take long (varies few seconds couple of hours), though of service make task , activity start/stop/monitor. organically similar mp3 player, why think possible.

however, when rotate screen, activity destroyed/created (this not problem), , service! (this is problem, because new service different service, different state). want service survive destruction of activity (by way, current reproducible problem rotation, may happen in other moment, such presing in activity or so).

1) asking impossible? hard me believe it, because want music player wants...

2) if (impossible) { other idea? }

3) else { idea of doing wrong? }

i apreciate "what doing right", since tell me problem in code, , not in concept.

here pseudocode (android 2.2, api 8):

edit1: added "return start_not_sticky", since may important...

// service class myservice extends service {   private myinterface.stub binder = new myinterface.stub() {     public int getprogress() {...}     public void dostop() {...}   }   oncreate() {     log("myservice created");   }   ondestroy() {     log("myservice destroyed");     dostop();   }   onbind() {     log("myservice bound");     return binder;   }   onstartcommand() {     (new thread() {       void run() {         while (!stop) {           //do long task         }         stopself();       }     }).start();     return start_not_sticky;   } }  // activity class myactivity extends activity {   myinterface _service;   serviceconnection conn = new serviceconnection() {     onserviceconnected(s) {       _service = myinterface.stub.asinterface(s);     }     onservicedisconnected() {       _service = null;     }   }   oncreate() {     log("myactivity created");     bindservice(myservice, conn);   }   ondestroy() {     log("myactivity destroyed");     unbindservice(conn);   }    clickstart() {     startservice(new intent(this, myservice));   }   clickstop() {     _service.dostop();   }   clickprogress() {     _service.getprogress();   } }  // manifest <application>   <activity name=".myactivity">   <service name=".myservice" android:process=":remote" /> </application> 

edit2: more information. pseudocode says:

class myservice extends service { ... } 

however, oversimplification, since is:

class myservice extends mybaseservice { ... } class mybaseservice extends service { ... } 

mybaseservice in different package , in different project (a "library project").

then, if not start/bindto myservice, mybaseservice (and change manifest acordingly), works expected.

it enough me, since behaviour implemented in myservice (in relation mybaseservice) coded mybaseservice, , controlled via putextra intent launches service (although breaks oop, don't mind).

however, still can't find why 1 works other not. report if progress further.

edit3: fixed pseudocode (duplicated oncreate). real code not have it.

edit4: notice on procedure:

1) starts expected.

2) user clicks start, click_start() run, service servicestart()'ed.

3) user changes orientation. myactivyty.ondestroy() called, causing unbindservice().

4) service destroyed; myservice.ondestroy() called.

5) activity re-created. myactivity.oncreate() called, causing bindservice().

6) service re-created; myservice.oncreate() called. state has been lost.

my problem step 4 (6 consecuence of 4).

of course it's not impossible. said, music application uses service doesn't killed when rotating phone.

the problem encounter following. service bound activity. every time screen orientation changes, android destroys current activity , recreates immediately. standard behavior of android when comes configuration changes screen orientation. service tied activity destroyed.

you got multiple options solve problem.

  • you can prohibit orientation changes setting activities orientation fix value. android:screenorientation="portrait"

  • you can define handle configuration changes activity adding android:configchanges="orientation" attribute activity in application manifest. when activity won't destroyed recreated, instead method onconfigurationchanged(configuration) called , have handle configuration changes if need to.

  • another way might store connection service anywhere outside of activity. think have use same service connection every time communicate service. if activity recreated after orientation change create new connection , new service guess. i'm not 100% sure know behavior asynctasks.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -