Saturday 15 September 2012

Broadcast Receiver


A broadcast receiver is a component that responds to system-wide broadcast announcements. Many broadcasts originate from the system—for example, a broadcast announcing that the screen has turned off, the battery is low, or a picture was captured. Applications can also initiate broadcasts—for example, to let other applications know that some data has been downloaded to the device and is available for them to use. Although broadcast receivers don't display a user interface, they may create a status bar notification to alert the user when a broadcast event occurs. More commonly, though, a broadcast receiver is just a "gateway" to other components and is intended to do a very minimal amount of work. For instance, it might initiate a service to perform some work based on the event.
Types of Broadcast:
Basically there are two types of BroadcastReceiver
1.    Normal BroadcastReceiver
2.    Ordered BroadcastReceiver

Normal BroadcastReceiver:
                        In normal broadcast receiver there is no particular order to receive broadcast.In Normal BroadcastReceiver we have to use sendBroadcast(intent).

Ordered BroadcastReceiver:
                        When you want to receive broadcast in particular order,then you need to use Ordered BroadcastReceiver and in receiver tags intent filter you have to include the priority. Depending on the prioity, it will call the receiver. In Ordered Broadcast we have to use sendOrderedBroadcast(intent).

Next We See how to create norml broadcast receiver with programming example.
Step1: 
           Create a new Project(Name: BroadcastTest)
Step2: 
          Go to your manifest.xml file. Observe in the bellow Application Tag.
       
         Click on Application =>Add => Choose Receiver => OK
         
       Now you need to give the name of the Receiver.Click on the name and give your receiver name.Here I am giving MyReceiver.Then Finish.


Step3:
       See our receiver has been created.Just put a Toast message to check our receiver is working or not. See bellow coding:
public class MyReceiver extends BroadcastReceiver {

            @Override
            public void onReceive(Context context, Intent intent)
            {
                        Toast.makeText(context, "Receiver is Started", Toast.LENGTH_LONG).show();
            }

}
Step4:
      Now this receiver we need to call from our main Activity class. So go your main Activity class BroadcastTestActivity . Open main.xml. Take a button.When we click on that button , receiver will be started.
BroadcastTestActivity.java
public class BroadcastTestActivity extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button btn = (Button)findViewById(R.id.button1);
        final Intent it = new Intent(getApplicationContext(), MyReceiver.class);
        btn.setOnClickListener(new OnClickListener()
        {
                                   
                                    @Override
                                    public void onClick(View v)
                                    {
                        //Sending broadcast
                                                sendBroadcast(it);
                                    }
                        });
    }
}
Step5: Execute your program.
Click on the button,receiver will start.

No comments:

Post a Comment