Custom Search

Broad Cast Receiver In Android

I know there are lots of tutorial present on Broadcast .But still not best for the who is beginning in android application development. As we know there are four basic component in android

1)Content Provider

2)Activity

3)Service

4)Broadcast Receiver

In this article i am going to explain about broad cast receiver

What exactly it is?
Definition: Broadcast is way of telling someone that now you have to that this is right time to do this.So do this.

ex..There is teacher in class.If he broadcast a message while teaching and he does not specify any specific audience then all receiver(Student and will act).But if specify any action to particular student then he will act.
See in the pic.Only student having name Tofeeq will act
Using Broadcast receiver in android:There are two ways to use broad cast receiver in android.one is using
dynamically and one in statically

in first way register receiver when you feel, now you have to send broad cast message
and then unregistered when task completed(As its very battery consuming process)

In second declare in Manifest file and then no need to register.

Step by step procedure:


1) Take a class and extends Broadcast receiver class in this..


public class DateChangesReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
//If you have more than one and on each action you want to perform diffe rent task then use if(intent.getAction.equals(Intent.action1)){ //do something }else if(intent.getAction.equals(Intent.action1)){ //do something }
}
}



2)Register BroadCastReceiver to Receiver broadcast action:
  In android Manifest

<receiver android:name="DateChangesReceiver "
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
//We can have more than one action here Like date change 
</intent-filter>
</receiver>


    In activity
   mMyReceiver is the object of Receiver Class,

IntentFilter filter=new IntentFilter();
//Set action to filter
 registerReceiver(mMyReceiver, filter);
Now after registration if you perform same action then BroasCastReceiver OnReceive() method will call.
complete Example from http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/appwidget/ExampleBroadcastReceiver.html

public class ExampleBroadcastReceiver extends BroadcastReceiver {
   
@Override
   
public void onReceive(Context context, Intent intent) {
       
Log.d("ExmampleBroadcastReceiver", "intent=" + intent);
     
// For our example, we'll also update all of the widgets when the timezone
       
// changes, or the user or network sets the time.
       
String action = intent.getAction();
       
if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)
               
|| action.equals(Intent.ACTION_TIME_CHANGED)) {
       
AppWidgetManager gm = AppWidgetManager.getInstance(context);
       
ArrayList<Integer> appWidgetIds = new ArrayList<Integer>();
       
ArrayList<String> texts = new ArrayList<String>();
       
ExampleAppWidgetConfigure.loadAllTitlePrefs(context,appWidgetIds s); final int N = appWidgetIds.size()
           
for (int i=0; i<N; i++) {
               
ExampleAppWidgetProvider.updateAppWidget(context, gm, appWidgetIds.get(i), texts.get(i));
           
}
       
}
   
}
}
See this For More Details, See This page for Action List In Android braodcast

0 comments:

Post a Comment