Monday, 19 September 2011

Snippets for BroadCast Receiver


BroadCast Receiver with Custom Intent.

private final String CUSTOM_INTENT="com.android.example.CUSTOM_INTENT" 

 Intent intent=new Intent();
 intent.setAction(CUSTOM_INTENT);//should be as package name
      
       
  BroadcastReceiver receiver=new BroadcastReceiver() {
                 
                  @Override
                  public void onReceive(Context context, Intent intent) {
                        // TODO Auto-generated method stub
                       
                  }
            };
  IntentFilter filter=new IntentFilter("Custom_Intent");
  registerReceiver(receiver, filter);
  sendBroadcast(intent);


BroadCast Receiver with System Events.

Step 1:
Add receiver to Android Manifest File:
<receiver android:name=".PhoneReceiver">
    <intent-filter>
                  <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
</receiver>

Step  2: Define your Broad cast Receiver
public class PhoneReceiver extends BroadcastReceiver {

      @Override
      public void onReceive(Context context, Intent intent) {
            //toDo
      }
}



Important Points:

  • Broadcast receiver should complete its onReceive() Method with in 5 sec.
  • Difference between sendStickyBroadcast() and sendBroadcast() is:


Perform a sendBroadcast(Intent) that is "sticky," meaning the Intent you are sending stays around after the broadcast is complete, so that others can quickly retrieve that data through the return value of registerReceiver(BroadcastReceiver, IntentFilter). In all other ways, this behaves the same as sendBroadcast(Intent).
One example of a sticky broadcast sent via the operating system is ACTION_BATTERY_CHANGED. When you call registerReceiver() for that action -- even with a null BroadcastReceiver -- you get the Intent that was last broadcast for that action. Hence, you can use this to find the state of the battery without necessarily registering for all future state changes in the battery.

  • android:pirority can be used to control broadcast intents between receiver if it was an ordered Broadcast.



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.