• 12
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Android 5.0 JobScheduler API on earlier versions?

I'm building an Android application, which makes use of a manifest broadcast receiver to know when the device is plugged in. I think Android 5.0 JobScheduler API would be a much better way of doing this, but I have one question: is it possible to use this API or a similar one on earlier versions of Android? If it is not possible, what would be the best option to do this (instead of a manifest broadcast receiver)?

Another option is this library: https://github.com/evernote/android-job

It uses the JobScheduler and GcmNetworkManager if possible. Otherwise it fallbacks to the AlarmManager. It also handles different API levels with the AlarmManager properly. This blog post gives you an overview.

(PS: I'm the main developer who worked on this library at Evernote. It's useful for our projects, that's why I wanted to share it here as well)

  • 23
Reply Report
    • great job man, I love this library. Unfortunately there isn't much documentation about it and I'm offen find myself not sure about the configuration of the job.
    • Actually, you shouldn't care much about the configuration. The documentation on Github explains everything that is needed. Creating an actual job also isn't hard. Create your Job class and schedule it with the Builder, which provides some easy to use parameters.

Update:

WorkManager was introduced to replace below.

Update:

Firebase JobDispatcher was introduced to replace below.


Update:

In Play Services 7.5 GCM Network Manager was introduced.

This is the closest approximation for a backport of the JobSchedulers API.


Evant user on Github is creating an Compat version of the JobScheduler API.

At time of posting it is not production ready but it is worth mentioning

  • 18
Reply Report

I think Android 5.0 JobScheduler API would be a much better way of doing this

Well, that depends on what "this" is. Simply knowing when the device is plugged in is pointless unless you are going to do something with that information, and we do not know what you have in mind.

Bear in mind that JobScheduler does not necessarily give you control when the device is plugged in. Rather, it uses that fact, plus your JobInfo and other environmental factors, to determine when to give you control.

is it possible to use this API or a similar one on earlier versions of Android?

There is no backport of JobScheduler supplied by the Android SDK at this time. While somebody will likely write a backport at some point, I am not aware of one available as open source at the moment either.

If it is not possible, what would be the best option to do this (instead of a manifest broadcast receiver)?

Again, it depends on what "this" really is. For example, if you are trying to say "I want to get control every so often, but only if the device is plugged in", you could use AlarmManager, then check to see whether the device is plugged in before continuing with your work.

  • 9
Reply Report
      • 1
    • Thank you! I simply want to do some work while phone is charging, to reduce battery consumption. The work is done inside an IntentService
      • 2
    • @user3194230: That definitely sounds like a good use case for JobScheduler (though you'd need to switch to a JobService for that). You can perhaps use ACTION_POWER_CONNECTED in the meantime, though -- I had forgotten about that broadcast when I wrote my answer.
    • Yes. Right now I'm using a manifest BroadcastReceiver that listens for the ACTION_POWER_CONNECTED broadcast. On my Nexus 5 running Android 4.4.4 it works as expected, but I didn't get it to work on my friend's Samsung Galaxy S (running Android 2.3.6). I have tried listening for the ACTION_BATTERY_CHANGED broadcast, and it works both in my phone and in my friend's one, but this broadcast requires my app to be running all the time
      • 1
    • @user3194230: "I didn't get it to work on my friend's Samsung Galaxy S" -- well, the broadcast was available then, so Samsung must have screwed something up. :-( "this broadcast requires my app to be running all the time" -- yes, because if they had to fork a bunch of processes every time the battery dropped a bit of charge, that would be bad for the battery. Hence, you have to have a running process.
      • 2
    • Thank you. 'Hence,you have to have a running process'. What would be the best way of implementing it? Maybe using an AlarmManager?

Important Update

For Now onward everybody should use WorkManager for deferrable background tasks.

  • This library is backward compatible
  • It use JobScheduler,FirebaseJobDispatcher or AlarmManager
  • No Need to depend on play service library.
  • Recommended by Google for deferrable background work.
  • Can use features like chaining, constraints etc.

https://developer.android.com/guide/background/#workmanager https://developer.android.com/topic/libraries/architecture/workmanager/

  • 2
Reply Report