Broadcast Receiver based Android interview questions and answers

CAN AN APPLICATION START ON REBOOT OR WHEN THE DEVICE IS BEING STARTED?

This question is important, since for many in-house applications the purpose will be to
block the device and allow only operating with one application.
Making usage of a BroadcastReceiver with an Intent Filter, as follows, can do this.

<receiver 
    android-permission="android.permission.RECEIVE_BOOT_COMPLETED"
    android:name="YourReceiver" >

    <intent-filter >
        <action
                android:name="android.intent.action.SCREEN_ON" />
        <action 
                android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>

</receiver>


Aaaaa


What are broadcast receivers? How is it implemented?

  • Broadcast Receiver is a mechanism using which host application can listen for System level events.
  • Broadcast receiver is used by the application whenever they need to perform the execution based on system events. Like listening for Incoming call, sms etc.
  • Broadcast receivers helps in responding to broadcast messages from other application or from the system.
  • It is used to handle communication between Android operating system and applications. 

It is implemented as a subclass of BroadcastReceiver class and each message is broadcaster as an Intent object.

1
2
3
public class MyReceiver extends BroadcastReceiver {
Public void onReceive(context,intent){}
}

2) How do you kill a receiver?

ans : abortBroadcast() method

3) What is a LocalBroadcastManagerDeveloper Android

4) Consider a situation in which you are downloading a large file in your app and suddenly the battery goes critically low, you don’t want the download to stop abruptly and would like to show the user a notification/dialog so that he can pause the download. How you will achieve this in android application?

For checking the status of BatteryLow and sending information to the user,  we can create a BroadCast Receiver who will send a triggers battery Low notification that you see on your mobile screen.

A Broadcast Receiver is an Android component which allows you to register for system or application events. Once Event occur it will Broadcast message to the user.

There are two steps to create a Broadcast Receiver:

  • Create a subclass of Android’s BroadcastReceiver.
  • Implement the onReceive() method: in our case ,for generate  battery low notification, the receiver is registered to Intent.ACTION_BATTERY_LOW event. As soon as the battery level falls below the defined level, this onReceive() method is called.
<receiver android:name=".SampleBroadcastReceiver">
<intent-filter>
        <action android:name=" android.intent.action.BATTERY_LOW "/>
    </intent-filter>
</receiver>
  • At last, we will call our Broadcast Receiver using Implicit Intent.
Intent intent = new Intent(this, Reciver.class);
sendBroadcast(intent);

Why isn’t it a good idea to start a thread when a BroadcastReceiver receives an Intent in its onReceive() method? How can you solve this issue?

As soon as the execution of onReceive() is finished, the system may kill the process at any time (and that will also kill the spawned thread)

Solution: A good way is to use a JobService instead of a thread. By doing so, the system knows that the process is still needed

Comments