0

Since AsyncTask, IntentSerrvice and JobIntentService are all deprecated, which tool or class should I go for in 2022?

I want to re-schedule alarms in a BroadcastReceiver after a device rebooted (since alarms get lost in the process). The task will most probably take < 1 min to finish. I just need the safety of it completing and not being killed off by the system.


The documentation on Broadcasts shows an (outdated) example with goAsync() and the deprecated AsyncTask.
But it also mentions JobService. Is that the replacement? What about WorkManager?

Big_Chair
  • 2,781
  • 3
  • 31
  • 58
  • "I want to re-schedule notifications in a BroadcastReceiver after a device rebooted" -- I am not certain what "re-schedule notifications" means, exactly, but you can enqueue some work with `WorkManager`. – CommonsWare Apr 25 '22 at 20:52
  • @CommonsWare Sorry I meant alarms with `AlarmManager` (got the wordings mixed up with iOS). This is the only place I need to do that, is it worth importing the `WorkManager` dependency for this use case? – Big_Chair Apr 25 '22 at 21:24
  • You might be able to use `JobScheduler` if you wanted. – CommonsWare Apr 25 '22 at 22:06
  • @CommonsWare It seems that it is encouraged to use `WorkManager` instead of `JobScheduler` directly https://stackoverflow.com/questions/50279364/android-workmanager-vs-jobscheduler – Big_Chair Apr 29 '22 at 11:18

1 Answers1

1

goAsync() return a PendingIntent - it mean you ask for android system extend time life of Broadcast receiver => goAsync() is used for short background task. Life time of BroadcastReceiver is very short, so... for long time background task, you must to change to other context has longer life time, such as: Service, JobService..etc. Example:

  1. BroadcastReceiver received intent
  2. BroadcastReceiver start a service, run worker thread to process long time task
  3. after worker thread finish, call finish service too

=========================================

class MyIntentService : Service() {
    private val handleThread = HandlerThread("MyThread")
    private lateinit var workerHandler: Handler
    override fun onCreate() {
        super.onCreate()
        handleThread.start()
        workerHandler = Handler(handleThread.looper)
    }

    override fun onDestroy() {
        workerHandler.removeCallbacksAndMessages(null)
        handleThread.quitSafely()
        super.onDestroy()
    }
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        val data = intent?.data
        workerTask(data)
        return START_NOT_STICKY
    }

    private fun workerTask(data: Uri?) {
        workerHandler.post {
            heavyTask(data)
            finishMyIntentService()
        }
    }

    private fun finishMyIntentService() {
        stopSelf()
    }

    private fun heavyTask(data: Uri?) {
        // to do heavyTask example
        for (i in 1..20)
        {
            Log.d("test","#heavyTask() $i")
            Thread.sleep(1000)
        }
    }

    override fun onBind(intent: Intent?): IBinder? {
        TODO("Not yet implemented")
    }
}
 

then startService from BroadCastReceiver

baka3k
  • 186
  • 4
  • Exactly, I understand that. My question is focused on your 2. point: **which** service should I use best for this case? – Big_Chair Apr 26 '22 at 08:03
  • 1
    we have many ways to handle it, for your case, i think you can use Service & Handlerthread - same as IntentService. Essence of Intentservice are Service & HandlerThread, you can find source code of IntentService to know the way of implementation – baka3k Apr 27 '22 at 13:33
  • If you add to your answer how to do it in some detail, I will accept it :) – Big_Chair Apr 28 '22 at 07:30
  • ok, hope this help – baka3k Apr 29 '22 at 02:34