Android “Broadcast Receiver” Frequently Asked Interview Questions Part 3 of. N
I Have taken most of the questions from people’s interview reviews from glassdoor and combining them in one place.
1.What is a broadcast receiver in android?
Ans: Android apps can send or receive broadcast messages from the Android system and other Android apps, similar to the publish-subscribe design pattern. These broadcasts are sent when an event of interest occurs. For example, the Android system sends broadcasts when various system events occur, such as when the system boots up or the device starts charging. Apps can also send custom broadcasts, for example, to notify other apps of something that they might be interested in (for example, some new data has been downloaded).
2.Why there is a broadcast receiver in android?
Ans: For event base programming we need a broadcast receiver, In android, there may be several situations where you want to do some task when some event happens in the system or in the other app or even in your app and you want to notify the user or want to perform some task as soon as the event happens.
3.What are the types of broadcast receivers(implicit and explicit)?
Ans: Implicit: An implicit broadcast is one that does not target your application specifically so it is not exclusive to your application. To register for one, you need to use an IntentFilter and declare it in your manifest. You need to do all of this because the Android operating system goes over all the declared intent filters in your manifest and sees if there is a match. Because of this behavior, implicit broadcasts do not have a target attribute. An example of an implicit broadcast would be an action of an incoming SMS message.
Explicit: An explicit broadcast is one that is targeted specifically for your application on a component that is known in advance. This happens due to the target attribute that contains the application’s package name or a component class name.
4.How does a broadcast receiver work?
Ans: In a nutshell, BR works on the observer pattern, Somewhere an event has happened, and who so ever subscribed to that event will be notified.
5.Steps to create a broadcast receiver?
- Create a class that extends Broadcast receiver.
- Provide implementation to onReceived method.
- Declare class in the manifest as well as register dynamically from activity or Fragment.
6.In which thread broadcast receiver works?
Ans: Main Thread.
7.How many ways are there to register the broadcast receivers?
Ans: Static (In manifest)& Dynamic/Runtime(In activity or fragment).
8.How to send the broadcast?
- Create an intent
- Define action
- add some data /optional
- send the broadcast as below
Intent intent=new Intent(getApplicationContext(),MyReceiver.class);
9.How many ways are there to send broadcasts?
Ans: There are 3 ways to send broadcast.
- Local Broadcast. 2. Normal Broadcast. 3. Ordered Broadcast.
10.Can one app send a broadcast to another app?
Ans: Yes by explicit broadcast.
11.How to restrict broadcast to only the current app?
Ans: you can define local broadcast or exported false.
12.What changes introduced in android oreo i.e 8?
Ans: If your app targets Android 8.0 or higher, you cannot use the manifest to declare a receiver for most implicit broadcasts (broadcasts that don’t target your app specifically). You can still use a context-registered receiver when the user is actively using your app.
13.Can we start a service from a broadcast receiver?
Ans: From android 10 you can’t do.
14.Can we start a service from a broadcast receiver?
Ans: Only Started and Intent Service you cant bind a service from Br.
Ans: These are broadcasts whose data is held by the system after being finished, so that clients can quickly retrieve that data without having to wait for the next broadcast.
16.When to go for unregistering the broadcast?
Ans: In onStop() method.
17.What happens if you don’t unregister the broadcast?
Ans: It will cause a Memory leak.
18.Priority in the broadcast receiver?
Ans: It controls the order in which broadcast receivers are executed to receive broadcast messages. Those with higher priority values are called before those with lower values. (The order applies only to synchronous messages; it’s ignored for asynchronous messages.) Use this attribute only if you really need to impose a specific order in which the broadcasts are received, or want to force Android to prefer one activity over others.
Points to remember:
- you can split multiple broadcasts by using the intent in on receive method of broadcast Receiver.
- You can set permissions also with broadcast receivers.
- For your own app always use Local Broadcast.
- After the
onReceive()of the receiver class has finished, the Android system is allowed to recycle the receiver.(Life cycle of BR)
Android 7.0 (API level 24) and higher don’t send the following system broadcasts:
Beginning with Android 8.0 (API level 26), the system imposes additional restrictions on manifest-declared receivers.
If your app targets Android 8.0 or higher, you cannot use the manifest to declare a receiver for most implicit broadcasts (broadcasts that don’t target your app specifically). You can still use a context-registered receiver when the user is actively using your app.
Beginning with Android 9 (API level 28), The
NETWORK_STATE_CHANGED_ACTION broadcast doesn't receive information about the user's location or personally identifiable data.
In addition, if your app is installed on a device running Android 9 or higher, system broadcasts from Wi-Fi don’t contain SSIDs, BSSIDs, connection information, or scan results. To get this infor
Before API level 11, you could not perform any asynchronous operation in the
onReceive() method, because once the
onReceive() method had been finished, the Android system was allowed to recycle that component. If you have potentially long running operations, you should trigger a service instead.
Since Android API 11 you can call the
goAsync() method. This method returns an object of the
PendingResult type. The Android system considers the receiver as alive until you call the
PendingResult.finish() on this object. With this option you can trigger asynchronous processing in a receiver. As soon as that thread has completed, its task calls
finish() to indicate to the Android system that this component can be recycled.
Thanks for reading give a clap if you like this