Skip to content

How to Use BroadcastReceiver's goAsync() Method in Kotlin

Kotlin, BroadcastReceiver, goAsync, Android Development2 min read

BroadcastReceivers are an essential part of Android development that allow us to receive and handle system or application events. However, when it comes to handling long-running operations or network calls in a BroadcastReceiver, things can get a bit tricky. Android provides a solution to this problem by introducing the goAsync method.

The goAsync method is a powerful tool that allows a BroadcastReceiver to perform long-running operations asynchronously. In this article, we will discuss how to use the goAsync method in Kotlin.

The Problem with Long-Running Operations

BroadcastReceivers are designed to be short-lived, and the system expects them to complete their work quickly. However, sometimes we need to perform long-running operations, such as network calls, database queries, or file I/O. If we try to perform these operations synchronously in a BroadcastReceiver, we risk blocking the main thread and causing the system to kill our app.

Introducing goAsync

The goAsync method is a way to tell the system that we need more time to perform our work. When we call goAsync, the system grants us a limited amount of time to execute our code before the BroadcastReceiver is considered finished. This time limit is called the "timeout" and is typically set to 10 seconds.

When we call goAsync, we get an instance of a PendingResult object. This object allows us to tell the system when our work is complete. We can use this object to either report success or failure back to the system.

Using goAsync

Let's take a look at an example of how to use the goAsync method in Kotlin. Suppose we have a BroadcastReceiver that receives an Intent and needs to perform a network call. Here's how we can use goAsync to perform this work asynchronously:

1class MyReceiver : BroadcastReceiver() {
2 override fun onReceive(context: Context, intent: Intent) {
3 val result = goAsync()
5 val url = ""
7 val client = OkHttpClient()
8 val request = Request.Builder()
9 .url(url)
10 .build()
12 client.newCall(request).enqueue(object : Callback {
13 override fun onFailure(call: Call, e: IOException) {
14 result.setResultCode(Activity.RESULT_CANCELED)
15 result.finish()
16 }
18 override fun onResponse(call: Call, response: Response) {
19 // Process the response...
20 result.setResultCode(Activity.RESULT_OK)
21 result.finish()
22 }
23 })
24 }

In this example, we call goAsync to indicate that we need more time to perform our network call. We then create an OkHttpClient instance and a Request object and use the enqueue method to perform the call asynchronously. When the call completes, we use the setResultCode method of the PendingResult object to report success or failure back to the system, and then call finish to indicate that our work is complete.

In Summary

The goAsync method is a powerful tool that allows us to perform long-running operations asynchronously in a BroadcastReceiver. By using goAsync, we can avoid blocking the main thread and ensure that our app stays responsive. When using goAsync, it's important to remember to call setResultCode and finish to report the result of our work back to the system.

By following the example code above, you can start using goAsync method in your own BroadcastReceiver implementation.