android - How keeping service when App is Killed instead restart? -


i using service counter (0 - 10) test service, when app killed, service has restarted instead continue.

in logcat shows:

04-05 17:25:47.497 25309-25309/? i/livro: oncreate 04-05 17:25:47.498 25309-25309/? i/livro: onstartcommand 04-05 17:25:48.531 25309-25325/com.example.augustoc.helloservice i/livro: helloservice executando...0 04-05 17:25:49.571 25309-25325/com.example.augustoc.helloservice i/livro: helloservice executando...1 04-05 17:25:50.611 25309-25325/com.example.augustoc.helloservice i/livro: helloservice executando...2 04-05 17:25:51.651 25309-25325/com.example.augustoc.helloservice i/livro: helloservice executando...3 04-05 17:25:52.692 25309-25325/com.example.augustoc.helloservice i/livro: helloservice executando...4 04-05 17:25:53.733 25309-25325/com.example.augustoc.helloservice i/livro: helloservice executando...5 04-05 17:25:54.771 25309-25325/com.example.augustoc.helloservice i/livro: helloservice executando...6 04-05 17:25:55.811 25309-25325/com.example.augustoc.helloservice i/livro: helloservice executando...7 04-05 17:25:56.851 25309-25325/com.example.augustoc.helloservice i/livro: helloservice executando...8 04-05 17:25:57.891 25309-25325/com.example.augustoc.helloservice i/livro: helloservice executando...9 04-05 17:25:57.891 25309-25309/com.example.augustoc.helloservice i/livro: ondestroy 

when close app, counter has restarted, want service continue.

in onstartcommand:

@override                                               // id deste serviÇo     public int onstartcommand(intent intent, int flags, int startid) {         log.i(tag,"onstartcommand");         count = 0;         running = true;          //delega para trhead         new workerthread().start();         return start_sticky;     } 

i try return start_sticky , start_not_sticky, both not works.

use constant when creating service. replace return start_sticky; return start_redeliver_intent add necessary logic. start_redeliver_intent if system kills service after onstartcommand() returns, recreate service , call onstartcommand() last intent delivered service. pending intents delivered in turn. suitable services actively performing job should resumed.

also please consider using jobscheduler,you construct these jobinfo objects , pass them jobscheduler schedule(jobinfo). when criteria declared met, system execute job on application's jobservice. identify jobservice meant execute logic job when create jobinfo jobinfo.builder(int, android.content.componentname).

the framework intelligent when receive callbacks, , attempt batch , defer them as possible. typically if don't specify deadline on job, can run @ moment depending on current state of jobscheduler's internal queue, might deferred long until next time device connected power source.


Comments