Sunday, 18 September 2011

Android BroadCast Receiver

Components of Application in Android
  1. Activity: An activity represents a single screen with a user interface.
  2. Services: A service is a component that runs in the background to perform long-running operations or to perform work for remote processes.
  3. Content Providers: A content provider manages a shared set of application data.
  4. Broadcast Receiver: A broadcast receiver is a component that responds to system-wide broadcast announcements.

What is Broadcast Receiver?

It is a listening class that can be notified whenever a specific type of system message is sent. These classes are called Broadcast Receiver.

Need of Broadcast Receiver

  • If you want Android to instaniate the object whenever an appropriate intent is sent, define the <receiver> in the Android Manifest.xml file.
  • Receiver will get called with their onReceive() Method.
  • Receiver can define the intent-filter tag under the receiver tag . which defines the type of intent it can receive.
   Receiver tag to read the phone state.
<receiver android:name=".PhoneReceiver" >
 <intent-filter>
   <action android:name="android.intent.action.PHONE_STATE">
</action>
 </intent-filter>
  </receiver>
 
 Receiver tag to read the custom intent.
  <receiver android:name=".CustomReceiver">
     <intent-filter>
        <action android:name="com.daffodil.android.intent.action.CUSTOM_INTENT">
</action>
     </intent-filter>
  </receiver>
 
 

Defining Broadcast Receiver:

There are two ways to make a broadcast receiver known to the system:
  1. One is to declare it in the manifest file with this element.
     
    <receiver android:name=".PhoneReceiver" >
          <intent-filter>
         <action android:name="android.intent.action.PHONE_STATE">
    </action>
          </intent-filter>
      </receiver>
     
  2. The other is to create the receiver dynamically in code and register it with the Context.registerReceiver() method.

There are two major classes of broadcasts that can be received:
  • Normal broadcasts (sent with Context.sendBroadcast) are completely asynchronous. All receivers of the broadcast are run in an undefined order, often at the same time. This is more efficient, but means that receivers cannot use the result or abort APIs included here.
  • Ordered broadcasts (sent with Context.sendOrderedBroadcast) are delivered to one receiver at a time. As each receiver executes in turn, it can propagate a result to the next receiver, or it can completely abort the broadcast so that it won't be passed to other receivers. The order receivers run in can be controlled with the android:priority attribute of the matching intent-filter; receivers with the same priority will be run in an arbitrary order. 



1 comment:

  1. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.Android Training in chennai with placement | Android Training in velachery

    ReplyDelete