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)?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Another option is this library: https://github.com/evernote/android-job
It uses the
JobScheduler
andGcmNetworkManager
if possible. Otherwise it fallbacks to theAlarmManager
. It also handles different API levels with theAlarmManager
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)
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 :)
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
Important Update
For Now onward everybody should use WorkManager for deferrable background tasks.
https://developer.android.com/guide/background/#workmanager https://developer.android.com/topic/libraries/architecture/workmanager/
Firebase JobDispatcher was introduced.
This replaces the old GCM Network Manager library.
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 yourJobInfo
and other environmental factors, to determine when to give you control.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.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.