JobService Android service never starts

I have a JobService that I would like to start periodically. At the moment, for testing, I use the trivial option:

public class SenderService extends JobService {

    @Override
    public boolean onStartJob(final JobParameters params) {
        new Thread(new Runnable(){
            public void run(){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                jobFinished(params, false);
            }
        }).start();

        return true;
    }

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

I plan the work as follows:

ComponentName serviceName = new ComponentName(this, SenderService.class);

JobInfo jobInfo = new JobInfo.Builder(JOB_ID, serviceName)
        .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
        .setRequiresDeviceIdle(false)
        .setRequiresCharging(false)
        .setPersisted(true)
        .setPeriodic(10000)
        .build();

scheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);

int result = scheduler.schedule(jobInfo);

if (result == JobScheduler.RESULT_SUCCESS) {
    Log.d(LOGTAG, "job created");
} else {
    Log.d(LOGTAG, "job not created");
}

But the work never begins. If I remove setPeriodic (), it will start (but only once of course).

What's happening?

+4
source share

Source: https://habr.com/ru/post/1677366/


All Articles