BroadcastReceiver
abstract class BroadcastReceiver
| kotlin.Any | |
| ↳ | android.content.BroadcastReceiver |
Base class for code that receives and handles broadcast intents sent by android.content.Context.sendBroadcast(Intent).
You can either dynamically register an instance of this class with android.content.Context#registerReceiver or statically declare an implementation with the <receiver> tag in your AndroidManifest.xml.
Summary
| Nested classes | |
|---|---|
| open |
State for a result that is pending for a broadcast receiver. |
| Public constructors | |
|---|---|
| Public methods | |
|---|---|
| Unit |
Sets the flag indicating that this receiver should abort the current broadcast; only works with broadcasts sent through |
| Unit |
Clears the flag indicating that this receiver should abort the current broadcast. |
| Boolean |
Returns the flag indicating whether or not this receiver should abort the current broadcast. |
| Boolean |
Return the last value given to |
| Int |
Retrieve the current result code, as set by the previous receiver. |
| String! |
Retrieve the current result data, as set by the previous receiver. |
| Bundle! |
getResultExtras(makeMap: Boolean)Retrieve the current result extra data, as set by the previous receiver. |
| open String? |
Returns the package name of the app that initially sent this broadcast. |
| open Int |
Returns the uid of the app that initially sent this broadcast. |
| BroadcastReceiver.PendingResult! |
goAsync()This can be called by an application in |
| Boolean |
Returns true if the receiver is currently processing the initial value of a sticky broadcast -- that is, the value that was last broadcast and is currently held in the sticky cache, so this is not directly the result of a broadcast right now. |
| Boolean |
Returns true if the receiver is currently processing an ordered broadcast. |
| abstract Unit |
This method is called when the BroadcastReceiver is receiving an Intent broadcast. |
| open IBinder! |
peekService(myContext: Context!, service: Intent!)Provide a binder to an already-bound service. |
| Unit |
setDebugUnregister(debug: Boolean)Control inclusion of debugging help for mismatched calls to |
| Unit |
setOrderedHint(isOrdered: Boolean)For internal use, sets the hint about whether this BroadcastReceiver is running in ordered mode. |
| Unit |
Change all of the result data returned from this broadcasts; only works with broadcasts sent through |
| Unit |
setResultCode(code: Int)Change the current result code of this broadcast; only works with broadcasts sent through |
| Unit |
setResultData(data: String!)Change the current result data of this broadcast; only works with broadcasts sent through |
| Unit |
setResultExtras(extras: Bundle!)Change the current result extras of this broadcast; only works with broadcasts sent through |
Public constructors
Public methods
abortBroadcast
fun abortBroadcast(): Unit
Sets the flag indicating that this receiver should abort the current broadcast; only works with broadcasts sent through Context.sendOrderedBroadcast. This will prevent any other broadcast receivers from receiving the broadcast. It will still call onReceive of the BroadcastReceiver that the caller of Context.sendOrderedBroadcast passed in.
This method does not work with non-ordered broadcasts such as those sent with Context.sendBroadcast
clearAbortBroadcast
fun clearAbortBroadcast(): Unit
Clears the flag indicating that this receiver should abort the current broadcast.
getAbortBroadcast
fun getAbortBroadcast(): Boolean
Returns the flag indicating whether or not this receiver should abort the current broadcast.
| Return | |
|---|---|
Boolean |
True if the broadcast should be aborted. |
getDebugUnregister
fun getDebugUnregister(): Boolean
Return the last value given to setDebugUnregister.
getResultCode
fun getResultCode(): Int
Retrieve the current result code, as set by the previous receiver.
| Return | |
|---|---|
Int |
int The current result code. |
getResultData
fun getResultData(): String!
Retrieve the current result data, as set by the previous receiver. Often this is null.
| Return | |
|---|---|
String! |
String The current result data; may be null. |
getResultExtras
fun getResultExtras(makeMap: Boolean): Bundle!
Retrieve the current result extra data, as set by the previous receiver. Any changes you make to the returned Map will be propagated to the next receiver.
| Parameters | |
|---|---|
makeMap |
Boolean: If true then a new empty Map will be made for you if the current Map is null; if false you should be prepared to receive a null Map. |
| Return | |
|---|---|
Bundle! |
Map The current extras map. |
getSentFromPackage
open fun getSentFromPackage(): String?
Returns the package name of the app that initially sent this broadcast.
| Return | |
|---|---|
String? |
the package name of the broadcasting app or null if the current receiver cannot access the identity of the broadcasting app |
getSentFromUid
open fun getSentFromUid(): Int
Returns the uid of the app that initially sent this broadcast.
| Return | |
|---|---|
Int |
the uid of the broadcasting app or Process.INVALID_UID if the current receiver cannot access the identity of the broadcasting app |
goAsync
fun goAsync(): BroadcastReceiver.PendingResult!
This can be called by an application in onReceive to allow it to keep the broadcast active after returning from that function. This does not change the expectation of being relatively responsive to the broadcast, but does allow the implementation to move work related to it over to another thread to avoid glitching the main UI thread due to disk IO.
As a general rule, broadcast receivers are allowed to run for up to 10 seconds before the system will consider them non-responsive and ANR the app. Since these usually execute on the app's main thread, they are already bound by the ~5 second time limit of various operations that can happen there (not to mention just avoiding UI jank), so the receive limit is generally not of concern. However, once you use goAsync, though able to be off the main thread, the broadcast execution limit still applies, and that includes the time spent between calling this method and ultimately PendingResult.finish().
If you are taking advantage of this method to have more time to execute, it is useful to know that the available time can be longer in certain situations. In particular, if the broadcast you are receiving is not a foreground broadcast (that is, the sender has not used Intent.FLAG_RECEIVER_FOREGROUND), then more time is allowed for the receivers to run, allowing them to execute for 30 seconds or even a bit more. This is something that receivers should rarely take advantage of (long work should be punted to another system facility such as android.app.job.JobScheduler, android.app.Service, or see especially androidx.core.app.JobIntentService), but can be useful in certain rare cases where it is necessary to do some work as soon as the broadcast is delivered. Keep in mind that the work you do here will block further broadcasts until it completes, so taking advantage of this at all excessively can be counter-productive and cause later event