Android 5.0 JobScheduler API on earlier versions?

前端 未结 6 1598
轻奢々
轻奢々 2021-02-01 02:40

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 mu

相关标签:
6条回答
  • 2021-02-01 02:54

    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)

    0 讨论(0)
  • 2021-02-01 02:54

    Firebase JobDispatcher was introduced.

    This replaces the old GCM Network Manager library.

    0 讨论(0)
  • 2021-02-01 02:55

    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

    0 讨论(0)
  • 2021-02-01 03:10

    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.

    0 讨论(0)
  • 2021-02-01 03:13

    Came across this library, didn't try it, but looks good for what you want: https://github.com/airk000/Trigger

    Evernote android-job might be a better solution though :)

    0 讨论(0)
  • 2021-02-01 03:20

    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/

    0 讨论(0)
提交回复
热议问题