JobSchedulerCompat icon indicating copy to clipboard operation
JobSchedulerCompat copied to clipboard

JobSchedulerCompat could easily drain the battery

Open soarcn opened this issue 10 years ago • 4 comments

Hi evant

Thanks for creating this library and bringing one of the best feature of android L to the real world.

I tried to apply JobSchedulerCompat in one of my project, but I soon give it up. cause it consume the battery with unacceptable speed (40% for one night)

I simplely create 3 jobs, each jobs will run a intentservice, there is only one periodic job, and all 3 jobs require network connection.

I check a little bit and turns out that jobscheduler will frequently wake the phone,and last for quite long time. This is a energy killer, I think without resolving this problem, this library could not used in real project ( I know you pointed this out in readme, but I do wish it could be better)

And another issue I try to arise is the sample application could not running correctly (at least) on android 2.3 because of android:permission="android.permission.BIND_JOB_SERVICE" , I don't know if I use this correctly, but not works here~~

soarcn avatar Jan 22 '15 15:01 soarcn

Hm, I suspected as much. Can you get any insight into which set of constraints is causing the frequent wakeups? Or, at the very least, could you post what your job setup looks like here?

The service should only run just long enough to run the jobs, but if you see it lasing a long time, maybe there is a bug causing it not to be stopped.

Also, exactly what error are you getting from android:permission="android.permission.BIND_JOB_SERVICE"?

evant avatar Jan 22 '15 16:01 evant

Hi evant,

I will update the job setup later.

The error will occur when running sample application on android 2.3 Caused by: java.lang.SecurityException: Not allowed to start service Intent { cmp=me.tatarka.support.job.sample/.service.TestJobService (has extras) } without permission android.permission.BIND_JOB_SERVICE at android.app.ContextImpl.startService(ContextImpl.java:920)

soarcn avatar Jan 23 '15 03:01 soarcn

Hi @evant,

Quite busy with works recently, and here comes my previous job configuration.

1 JobService------->

    /**
     * When the app's MainActivity is created, it starts this service. This is so that the activity
     * and this service can communicate back and forth. See "setUiCalback()"
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_NOT_STICKY;
    }

    @Override
    public boolean onStartJob(JobParameters params) {
        // each one will start a intent service with some network background  job
        switch (params.getJobId()) {
            case FEEDSYNC:
                JobService.add(new FeedUpdateJob());
                break;
            case OFFLINESORT:
                JobService.add(new OfflineSortNeoJob());
                break;
            case APPUPLOAD:
                JobService.add(new AppUploadNeoJob());
                break;
            case ONLINESORT:
                JobService.add(new OnlineSortNeoJob(true));
                break;
        }
        return true;
    }

------<

2 Schedule jobs ----->

 private void scheduleJobs(int period) {
        JobInfo.Builder builder = new JobInfo.Builder(SchedulerService.FEEDSYNC, mServiceComponent);
        builder.setRequiredNetworkType(InsightPreferences.getInstance().isFeedSyncOnlyWifi() ? JobInfo.NETWORK_TYPE_UNMETERED : JobInfo.NETWORK_TYPE_ANY)
                .setPeriodic(period * 60 * 1000);
        jobScheduler.schedule(builder.build());
    }

    private void scheduleJobs() {
        scheduleJobs(15);
        JobInfo appUploadJob = new JobInfo.Builder(SchedulerService.APPUPLOAD, mServiceComponent)
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                .build();
        jobScheduler.schedule(appUploadJob);

        JobInfo onlineSortJob = new JobInfo.Builder(SchedulerService.ONLINESORT, mServiceComponent)
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                .build();
        jobScheduler.schedule(onlineSortJob);
    }

------>

  • scheduleJobs() will be called when application launched

After I switch to syncadapter solution, the battery consuming has significantly dropped. from 40% to less 4% per one night

Is there anything wrong with my code?

soarcn avatar Feb 06 '15 13:02 soarcn

Hi @soarcn ! Hi @evant ! Any answer about the battery consuming problem you noticed? I observed a similar huge waste of battery too, when running it on Kitkat...

julioventura avatar Aug 25 '15 01:08 julioventura