android.app.job.JobScheduler instead of WakefulBroadcastReceiver usage
Asked Answered
P

1

8

As here described the usual class WakefulBroadcastReceiver is deprecated.

So, now this is not possible to create a scheduled tasks like for previous SDK releases. Google says, I have to use

https://developer.android.com/reference/android/app/job/JobScheduler.html

But how?

So, what's the most useful solution for background scheduled tasks nowadays?

Is AlarmManager not longer useful?

My application wakes up several times a day and updates the notifications using AlarmManager. I'm not really sure what I have to change. What I have to implement? The old API and a new one? Or only up-to-date solution?

Pratt answered 30/8, 2017 at 15:14 Comment(0)
T
5

now this is not possible to create a scheduled tasks like for previous SDK releases

WakefulBroadcastReceiver has never been the sole option for this. For example, my WakefulIntentService dates back to 2009. And the JobIntentService added to the support library supplants both of those.

But how?

Um, JobScheduler is covered in the JavaDocs and elsewhere in the documentation, as well as books and courses on Android app development.

what's the most useful solution for background scheduled tasks nowadays?

That depends entirely upon how you want to define "useful", and I don't know your definition.

Generally speaking:

  • Use JobScheduler where possible

  • Use setAlarmClock() on AlarmManager if you are writing an alarm clock app

  • Use the other set...() methods on AlarmManager for backwards compatibility for pre-Android 5.0 devices, perhaps via a wrapper library (e.g., Evernote's android-job)

  • Use push messaging (e.g., FCM) where practical and if you are in position to be dependent upon that push messaging solution

Is AlarmManager not longer useful?

Doing any sort of periodic background work is "no longer useful" for some definitions of "useful". Doze mode and app standby, introduced in Android 6.0, basically make periodic background work unreliable from the user's standpoint.

What I have to implement? The old API and a new one? Or only up-to-date solution?

It is impossible to answer that in the abstract. There is nothing in Android 8.0, or the support library, that prevents you from using AlarmManager as you have been.

Thorwald answered 30/8, 2017 at 16:20 Comment(7)
I have to use WakefulBroadcastReceiver to listen to AIRPLANE_MODE changes and then disable some feature which is running in background in some third party SDK, even when app is in background. I am confused how to use Jobscheduler to listen to AIRPLANE_MODE change when my app is in background.Fpc
@srv_sud: That broadcast action does not appear on the implicit broadcast whitelist, so that may not work on Android 8.0+ anyway. Beyond that, I recommend that you ask a separate Stack Overflow question regarding your concerns.Thorwald
@CommonsWare, really, there is no tutorials in the internet "how to use Jobscheduler instead of WakefulBroadcastReceiver" to catch push notifications. A lot of examples how to replace Service via JobScheduler which is being started by wakefulreceiver but it is not the same.Loehr
@DennisZinkovski: This question is not about push notifications. FCM-style push is one alternative to on-device scheduling options, and it is the direction that Google would like you to go, but it will not make sense in all situations. And JobScheduler has nothing to do with push notifications.Thorwald
@CommonsWare, yes, I know, you mentioned that jobscheduler can be used with push notifications. WakefulBroadcastReceiver is also popular to catch events from server to show pushes, AS shows it's deprecated solution, so I am seeking how to replace it using JobScheduler and found nothing useful. I have a legacy code with GCM-pushes, currently we're updating client according to new android api's.Loehr
@CommonsWare, All I want to say, that WakefulBroadcastReceiver (as I see) cannot be replaced with JobScheduler (as android studio says) if we talk about push-notification, but Service could be. Am I right?Loehr
@DennisZinkovski: "you mentioned that jobscheduler can be used with push notifications" -- not in this answer, or anywhere else that I can think of. "Am I right?" -- JobIntentService is a replacement for IntentService. JobScheduler is a replacement (of sorts) for AlarmManager. JobScheduler is not a general-purpose replacement for Service.Thorwald

© 2022 - 2024 — McMap. All rights reserved.