1

An application is having one broadcast receiver - whose onReceive() is currently executing, service - is in running state; then what is that process priority?

  • service process priority (3)
  • empty process(5)
  • foreground process(1)
  • visible process(2)

Answer: C foreground process(1)
Description: Receiver which is executing its onReceive() will be treated with highest priority. process priority will be the maximum of components priority. So in this case process priority is highest priority.

2

What is broadcastreceiver in android?

  • It is a component of android which responds to system wide broadcast announcments.
  • It acts like a gateway between outside world and your application.
  • both are true
  • none

Answer: C both are true
Description:

3

Broadcast receiver runs in which thread, by default?

  • Main Thread
  • background thread
  • No thread

Answer: A Main Thread
Description: Default every component of android will run in Main thread (UI thread). So broadcast receiver also runs in main thread by default.

4

What will happen if broad cast receiver binds to binder service? Is there any problem?

  • No, One should not bind a service from Broadcast receiver.
  • Option 1 is right because, broadcast receivers will have a time limit of 10 seconds. establishing connection to a service may take more time.
  • No problem will occur if one binds a service from broad cast receiver.
  • Option 3 is right because, receivers doesn't have any time limit to finish its functionality.

Answer: B Option 1 is right because, broadcast receivers will have a time limit of 10 seconds. establishing connection to a service may take more time.
Description: One should not bind a service from Broadcast receiver. The reason is broadcast receivers are light weight components, where it has to finish its functionality with in not more than 10 seconds maximum. Else android may forcefully kill your receiver. Binding (establishing connection to) a service may take more than 10 seconds in some worst cases, that's why android won't allow it.

Rules for Broadcast Receivers:
1.Broadcast receivers will not have any UI(mostly) and it will have only background logic.
2.Broadcast receivers will have the maximum time limit of 10 sec to finish its functionality otherwise it will crash.
3.You should not do long running operations or asynchronous operations in the receiver.
Example: a. Preparing SD card. b. Uploading / Downloading files from internet. c. Creating Db files. d. Binding to services
4.Do not show dialog to the user in broadcast receiver. (this is asynchronous operation)
5.You can use “ toast” or “Notifications”.
6.Don’t write any heavy functionalities.

5

Is it possible to start a service from a broadcast receiver?

  • yes can start using startService() function
  • No, never start a service from a broadcast receiver

Answer: A yes can start using startService() function
Description: Any component can communicate with other component. only exception for this rule is don't bind to a service from a broad cast receiver. other wise one is free to start a service.

6

What is the difference between broadcast receiver and a service?

  • BroadcastReceiver - is like gateway for other components, can do small back ground functionality with in 10 seconds. Services - can do long running operation in the background with out having UI, and no time limit for it.
  • Service - is like gateway for other components, can do small back ground functionality with in 10 seconds. BroadcastReceiver - can do long running operation in the background with out having UI, and no time limit for it.
  • Option 1 is right, but a service can interact with UI if it wants. Its not mandatory that service should not have UI.
  • Options 1 is right, but both receiver and service both can interact with UI if they want to. Broadcast Receivers have time limit of 10 seconds, and they respond to broadcasted messages.

Answer: D Options 1 is right, but both receiver and service both can interact with UI if they want to. Broadcast Receivers have time limit of 10 seconds, and they respond to broadcasted messages.
Description: BroadcastReceiver - is like gateway for other components, can do small back ground functionality with in 10 seconds. Services - can do long running operation in the background with out having UI, and no time limit for it.but both receiver and service both can interact with UI if they want to. services will not have time limit of 10 seconds, receivers respond to broadcasted messages.

7

How to start a broadcast receiver on phone boot completed?

  • <receiver >
    <intent-filter >
    <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
    </receiver>
  • <receiver >
    <intent-filter >
    <action android:name="intent.action.BOOT_COMPLETED"/>
    </intent-filter>
    </receiver>
  • <receiver >
    <action android:name="intent.action.BOOT_COMPLETED"/>
    </receiver>
  • <broadcastreceiver >
    <intent-filter >
    <action android:name="intent.action.BOOT_COMPLETED"/>
    </intent-filter>
    </broadcastreceiver>

Answer: A <receiver >
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Description:

8

What is the time limit of a broadcast receiver, what will happen if it crosses that time limit?

  • 10 sec
  • 15 sec
  • 5 sec
  • 2 sec

Answer: A 10 sec
Description: Broad cast Receiver is a component that responds to system wide broad cast announcements. It acts as gateway to other components of our application. It is not supposed to do long running operation, it has to do its operation in 10 seconds maximum.

9

What is the difference between sendbroadcast(), sendorderedbroadcast(), sendstickybroadcast() ?

  • sendbroadcast() - normal broadcast
    sendorderedbroadcast() - we can set priority
    sendstickybroadcast() - intent passed with this will be stick for future users
  • sendbroadcast() - normal broadcast, but we can set priority as well.
    sendorderedbroadcast() - we can set priority, and set result. can't block broadcasts.
    sendstickybroadcast() - intent passed with this will be stick for future users
  • sendbroadcast() - normal broadcast, but we can set priority as well.
    sendorderedbroadcast() - we can set priority, and set result. can block broadcasts as well.
    sendstickybroadcast() - intent passed with this will be stick for future users
  • sendbroadcast() - normal broadcast, but we can set priority as well.
    sendorderedbroadcast() - we can set priority, and set result. can block broadcasts.
    sendstickybroadcast() - intent passed with this will be available for only one time.

Answer: C sendbroadcast() - normal broadcast, but we can set priority as well.
sendorderedbroadcast() - we can set priority, and set result. can block broadcasts as well.
sendstickybroadcast() - intent passed with this will be stick for future users
Description: sendbroadcast() - normal broadcast, but we can set priority as well.

sendorderedbroadcast() - we can set priority, and set result. can block broadcasts as well.
In the ordered broadcast you can predict the order of broadcast receiver using priority in the intent_ Filter.
1.If the priority is same for two receivers then order cannot be predicted.
2.In the ordered broadcast you can also pass data between two receivers.
3.You can also abort the broadcast anywhere in between the receivers.

sendstickybroadcast() - intent passed with this will be stick for future users who are registering through code (dynamic receivers).
When somebody sends a sticky broadcast using send stickyBroadcast(in); then that broadcast will be available for the future users who are using dynamic receivers.
This broadcast will be available for only Dynamic Broadcast rx who are coming in future.
Eg for stickybroadcast is - BATTERY LOW.

10

What is the life cycle of a broadcast receiver in android?

  • onReceive()
  • onCreate() -> onReceive()
  • onCreate() -> onReceive() -> onDestroy()
  • onReceive() -> onDestroy()

Answer: A onReceive()
Description: there is only one function in broad cast receiver, that is onReceive(). Broadcast receiver's life will start before calling onReceive() method, and once control comes out of onReceive() method, then it will be killed.

11

Under what thread broad cast receiver will run?

  • Worker thread
  • Kernel thread
  • Main Thread
  • No thread

Answer: C Main Thread
Description: By default all components run in Main thread give to your application.

12

To notify something to the user from a broadcast receiver, should I use dialogs or notifications? Why?

  • Use Dialogs, because they are more visible than Notifications.
  • Use Notifications, cause Receivers has to finish its functionality in 10 seconds
  • Use notifications, because showing Dialog all of sudden may disturb user.
  • Use both Dialogs and Notifications for safe side.

Answer: B Use Notifications, cause Receivers has to finish its functionality in 10 seconds
Description: Broadcast receivers are light weight components, which has to finish off its functionality with in time limit of 10 seconds. if we show dialog to user, there is a chance that user may take more than 10 seconds to respond to it by chance. in that case receiver may crash or may not be available to take dialog input from user. So it is always a standard and good practice to use notifications from a receiver.

13

How to create a receiver without registering it in manifest file?

  • Every component has to get registered in the manifest file.
  • We can register receiver dynamically in code.
  • we can register receiver statically in code.
  • option 1 is true because, with out intent-filter test it is not possible to launch code.

Answer: C We can register receiver dynamically in code.
Description: Every component has to get registered in the manifest file. But there is an exception for this rule, a broad cast receiver can get registered dynamically in code. Because there may be some scenarios where we need handle broad casted messages only when our application is running. If we register it in manifest file statically, then even if our app is not running, it may trigger our broad cast receiver.

14

How to send BATTERY_LOW broadcast? should I use sendbroadcast() or sendstickybroadcast? Why?

  • Use sendBroadCast(), because for sticky you may need to take permission.
  • use sendStickyBroadCast(), because logically this broadcast has to be available for future users.
  • We can use either, no harm.
  • use sendOrderedBroadcast(), because it has to be ordered properly.

Answer: B use sendStickyBroadCast(), because logically this broadcast has to be available for future users.
Description: We have to use sendStickyBroadCast() because, logically if battery went down, then this information has to be available for applications which may run after some time in future.

15

How set an alarm to trigger after two days? how to implement it? assume that user may switch off the phone in between.

  • Use AlarmManager and call set() to set after 2 days. Even phone switches off nothing will happen.
  • No, save it in onSaveInstanceStateUse AlarmManager and call set() to set after 2 days. If phone switched off, all alarms will be dead. So it is not possible.
  • Option 2 is right, but it can be reset after switching on, before switching off maintain all alarms details in Data Base and recreate it after switching on.
  • Option1 is true, because when phone got switched off all alarms will be dead, but when phone is switched on android will re created alarms automatically.

Answer: C Option 2 is right, but it can be reset after switching on, before switching off maintain all alarms details in Data Base and recreate it after switching on.
Description: Use AlarmManager and call set() to set after 2 days. If phone switched off, all alarms will be dead. So, before switching off maintain all alarms details in Data Base and recreate all alarms after switching on.

16

Trigger broadcast receiver only if my activity is in memory, else it should not get triggered, how to do it?

  • Send broadcast only when your activity is alive.
  • Register a dynamic receiver in that Activity.
  • Register a static receiver in manifest file
  • It is not possible to achieve

Answer: B Register a dynamic receiver in that Activity.
Description: If you want to trigger broadcast receiver as long as activity is in memory, then go for dynamic receiver which gets registered only when activity comes into memory. and unregister it before killing your activity.

17

How to make a receiver which will get triggered immediately after booting.

  • <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
    <receiver android:name="BroadcastReceiver">
    <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED"></action>

    </intent-filter>
    </receiver>
  • <receiver android:name="BroadcastReceiver">
    <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED"></action>

    </intent-filter>
    </receiver>
  • <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
    <receiver android:name="BroadcastReceiver">
    <intent-filter>
    <action android:name="Intent.BOOT_COMPLETED"></action>

    </intent-filter>
    </receiver>
  • <receiver android:name="BroadcastReceiver">
    <intent-filter>
    <action android:name="Intent.BOOT_COMPLETED"></action>

    </intent-filter>
    </receiver>

Answer: A <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<receiver android:name="BroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>

</intent-filter>
</receiver>
Description: