Broadcast Receiver Flashcards
What is BroadcastReceiver?
It receives broadcast events from either other apps or Android system.
How do you get a complete list of system broadcast actions?
See the BROADCAST_ACTIONS.TXT in Android SDK
What are the changes made to BroadcastReceivers since Android 7(Nougat)?
Android does not send ACTION_NEW_PICTURE and ACTION_NEW_VIDEO
You need to use context-registered broadcast receiver for CONNECTIVITY_ACTION
What are the changes made to BroadcastReceivers since Android 8?
You need to use context-registered receivers for most implicit broadcasts
What are the changes made to BroadcastReceivers since Android 9?
NETWORK_STATE_CHANGED_ACTION does not receive user location or PII data
System does not send SSIDs, BSSIDs, connection info, or scan results. Use getConnectionInfo() instea
How do you register a BroadcastReceiver using context?
- Subclass BroadcastReceiver and implement onReceive(Context, Intent)
- Create an instance of it
- Create an IntentFilter and register the receiver by calling registerReceiver(BroadcastReceiver, IntentFilter)
Under what condition a BroadcastReceiver receives an event?
As long as the context is valid.
If it’s registered in an Activity, it’s valid until onDestroy() is called.
If it’s registered in an Application, it’s valid as long as your app is running.
When should you unregister a BroadcastReceiver?
onDestroy() if you registered in onCreate()
onPause() if you registered in onResume()
When is your BroadcastReceiver is considered to be a foreground process?
When the code in onReceive() is being executed.
Under what condition should you consider using background thread?
If the task is expected to run more than 16ms
How should you manage a long running task invoked by your BroadcastReceiver?
You should use either goAsync() or JobScheduler
What are the 3 ways to send a broadcast?
- sendOrderedBroadcast(Intent, String)
- sendBroadcast(Intent)
- LocalBroadcastManager.sendBroadcast()