Skip to content
This repository has been archived by the owner on Jul 25, 2019. It is now read-only.

JobSchedulerCompat could easily drain the battery #15

Open
soarcn opened this issue Jan 22, 2015 · 4 comments
Open

JobSchedulerCompat could easily drain the battery #15

soarcn opened this issue Jan 22, 2015 · 4 comments

Comments

@soarcn
Copy link

soarcn commented Jan 22, 2015

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~~

@evant
Copy link
Owner

evant commented Jan 22, 2015

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"?

@soarcn
Copy link
Author

soarcn commented Jan 23, 2015

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
Copy link
Author

soarcn commented Feb 6, 2015

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?

@julioventura
Copy link

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...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants