GoAsync in BroadcastReceiver

43 views Asked by At

I'm trying to figure out what goAsync() does, but I can't. I thought this code would not work, but the broadcast works more than 2 minutes. Can you show me an example where goAsync() gives at least some advantages

class GoAsyncReceiver(binding: FragmentReceiverBinding) : BroadcastReceiver() {
    private val bindingRef = WeakReference(binding)
    override fun onReceive(context: Context, intent: Intent) {
        Log.d(TAG, "onReceive")
        val pendingResult = goAsync()
        thread {
            for (i in 0..120) {
                Thread.sleep(1000)
                Log.d(TAG, "$i")
                bindingRef.get()?.root?.post {
                    bindingRef.get()?.goAsyncResult?.text = i.toString()
                }
            }
            Log.d(TAG, "finish")
            pendingResult.finish()
        }
    }

    companion object {
        private const val TAG = "GoAsyncReceiver"
    }
}
0

There are 0 answers