Thursday, December 26, 2013

Ordered broadcast receivers:Android tutorial

Program description:

This is a sample demo program for ordered broadcast receivers in Android.

This program contains one activity, and 3 receivers.
Activity has a button, on clicking which we will send an ordered broadcast with implicit intent with action com.techpalle.RECEIVERS. All the 3 receiver will be having same intent filter with this matching action in them. But only difference is Receiver3 has priority-10, Receiver1 has priority-3, Receiver2 has priority-2. So the order of execution is with highest priority number to least priority number. 

So, Receiver3 executed first, then Receiver1 and then Receiver2. 

Note: If multiple receivers have same priority then android will choose to execute them in any random order.

Types of broadcasts: 3 types
1. Normal broadcasts - by using sendBroadcast()
2. Ordered broadcasts - by using sendOrderedBroadcast()
3. Sticky broadcasts - by using sendStickyBroadcast()

Normal broadcasts: by using sendBroadcast() method we can send normal broadcasts with an intent. If multiple components (receivers) are satisfying the intent given in the broadcast then android will start all the receivers in any order (randomly or simultaneously or one by one). 
Check this link for normal broadcasts

Ordered broadcasts: With ordered broadcasts programmer has the control over the order in which a receiver can be executed.
You can prioritize the order by giving priority attribute in the intent-filter tag of receiver tag in manifest file. More the priority number, higher the priority.
Along with prioritizing the order, we can also abort a broadcast in between and stop it going to the next receivers. With normal broadcasts we don't have this option.

Sticky broadcasts: Intent that is broadcasted with sendStickyBroadcast() will be stick with android for future users. (Future registerReceiver requests).

First Activity:

package com.techpalle.b15_rectypes;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
 Button b;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  b = (Button) findViewById(R.id.button1);
  b.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    Intent in = new Intent();
    in.setAction("com.techpalle.RECEIVERS");
    sendOrderedBroadcast(in, null);
   }
  });
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}

xml file for first activity 
File name : activity_main.xml


 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:paddingBottom="@dimen/activity_vertical_margin"  
   android:paddingLeft="@dimen/activity_horizontal_margin"  
   android:paddingRight="@dimen/activity_horizontal_margin"  
   android:paddingTop="@dimen/activity_vertical_margin"  
   tools:context=".MainActivity" >  
   <TextView  
     android:id="@+id/textView1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="@string/hello_world" />  
   <Button  
     android:id="@+id/button1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignLeft="@+id/textView1"  
     android:layout_below="@+id/textView1"  
     android:layout_marginTop="39dp"  
     android:text="Send broadcast.." />  
 </RelativeLayout>  

Broadcast Receiver
File name : Receiver1.java


package com.techpalle.b15_rectypes;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class Receiver1 extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
     // TODO Auto-generated method stub
  String res = getResultData();
  Toast.makeText(context, "RCVR 1.."+res, 0).show();
 }
}

Broadcast Receiver
File name : Receiver2.java

package com.techpalle.b15_rectypes;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class Receiver2 extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
     // TODO Auto-generated method stub
  String res = getResultData();
  Toast.makeText(context, "RCVR 2.."+res, 0).show();
 }
}

Broadcast Receiver
File name : Receiver3.java

package com.techpalle.b15_rectypes;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.widget.Toast;

public class Receiver3 extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
     // TODO Auto-generated method stub
  Toast.makeText(context, "RCVR 3", 0).show();
  //abortBroadcast();
  setResultData("Hey..");
 }
}

Manifest file
File name : AndroidManifest.xml


 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.techpalle.b15_rectypes"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="17" />  
   <application  
     android:allowBackup="true"  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
     <activity  
       android:name="com.techpalle.b15_rectypes.MainActivity"  
       android:label="@string/app_name" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
     <receiver android:name="Receiver1">  
       <intent-filter android:priority="3">  
         <action android:name="com.techpalle.RECEIVERS"/>  
       </intent-filter>  
     </receiver>  
     <receiver android:name="Receiver2">  
       <intent-filter android:priority="2">  
         <action android:name="com.techpalle.RECEIVERS"/>  
       </intent-filter>  
     </receiver>  
     <receiver android:name="Receiver3">  
       <intent-filter android:priority="10">  
         <action android:name="com.techpalle.RECEIVERS"/>  
       </intent-filter>  
     </receiver>  
   </application>  
 </manifest>  

Download complete code : Click to download

Android interview questions on Ordered broadcast receivers:
Difference between Normal broadcast, ordered broadcast, and sticky broadcast?

Tags: android, ordered, broadcast receivers, priority

SD cards with best prices:

1 comment: