Android O: Replace sticky service with jobdispatcher -


since android o not allow background services run, did choose different approach run new thread in background using firebase jobscheduler, see: https://github.com/firebase/firebase-jobdispatcher-android

at moment, start background thread in service. sticky service, , restart if user closes app recents (when slide remove app running apps).

public class notificationservice extends service {  @override public void oncreate() {     // start thread     applicationloader.postinitapplication(); }  @override public int onstartcommand(intent intent, int flags, int startid) {     return start_sticky; }  @override public ibinder onbind(intent intent) {     return null; } 

since android o not allow service started in background, implemented jobdispatcher. works fine, problem is:

how can restart job when app closed recents?

starting job:

firebasejobdispatcher dispatcher = new firebasejobdispatcher                 (new googleplaydriver(applicationcontext));          job notificationjob = dispatcher.newjobbuilder()                 .setservice(notificationservice.class)                 .settag("backgroundjob")                 .setrecurring(false)                 .setlifetime(lifetime.until_next_boot)                 .settrigger(trigger.now)                 .build();         dispatcher.mustschedule(notificationjob); 

the service:

public class notificationservice extends jobservice {  @override public boolean onstartjob(jobparameters job) {     // check if thread started, if not start thread     applicationloader.initnotificationthread();     return false; }  @override public boolean onstopjob(jobparameters job) {     return false; } 

one workaround problem make job recurring using .setrecurring(true), , set .settrigger 1 minute. job can check if thread running already, , if not start thread. keeps working, if user removes app recents.

however, solution feels uncomfortable start check each time while isn't needed.

is there better way solve this?


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -