Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a JobScheduler execute a foreground service?

Is it possible? I need to start my foreground service because I'm using it with the UI thread,

Example: Every 25minutes play a video in my app, which is it closed.

like image 721
Pablo Cegarra Avatar asked Mar 15 '18 21:03

Pablo Cegarra


1 Answers

I think that this is not the best use case to use JobScheduler, but you can! First of all you should create

public class ExerciseJobService extends JobService {
    private Context context;
    public ExerciseJobService(Context context) {
         this.context = context
    }

    @Override
    public boolean onStartJob(JobParameters params) {
        Intent intent = new Intent(context, YOUR_SERVICE.class)
        startForegroundService(intent)
        return true;
   }

    @Override
    public boolean onStopJob(JobParameters params) {
        return true;
    }
}

After that schedule Job

private FirebaseJobDispatcher jobDispatcher = FirebaseJobDispatcher(GooglePlayDriver(context)) 
Job myJob = jobDispatcher.newJobBuilder()
                .setService(ExerciseJobService::class.java)
                .setTag("SIMPLE_TAG")
                .setTrigger(Trigger.NOW)
                .build()
jobDispatcher.mustSchedule(myJob)
like image 69
a23sokolov Avatar answered Oct 08 '22 02:10

a23sokolov