Breaking News

31. Working with BroadcastReceiver in android

Working with BroadcastReceiver in android
Broadcast Receiver

Introduction to Broadcast Receiver

⬜ BroadcastReceiver is one of the basic and important components of the android application.
  ⬜ A BroadcastReceiver is a dormant component of android that responds to different messages that are broadcast by the system or other application.

  ⬜ Such messages are broadcast as an intent object that is waiting for broadcast receivers to respond.

  ⬜ The broadcast receiver responds to such broadcasted and takes the necessary actions.

  ⬜ Unlike activities, a BroadcastReceiver doesn't contain any user interface.It's generally implemented to delegate the tasks to service depending on the type of intent data that are received;

Some important System broadcast

BroadcastReceiver in android
System broadcast

Setting up a BroadcastReceiver

⬜To set up a BroadcastReceiver in our android application we need to do the following two things.

         ◼️Creating a BroadcastReceiver

          ◼️Registering a BroadcastReceiver

             ▫️Statically in the manifest file.

             ▫️Dynamically in the code.

Creating A BroadcastReceiver

⬜A broadcast receiver is implemented as a subclass of BroadcastReceiver class and overriding the onReceive() method where each message is received as an Intent object parameter.

public class MyReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Action: " + intent.getAction(), Toast.LENGTH_SHORT).show();

⬜BroadcastReceiver is an abstract class with the onReceiver() method being abstract.

⬜The onReceive() method is first called on the registered BroadcastReceivers when an event occurs.

⬜The Intent object is passed with all the additional data. 

⬜A Context object is also available and is used to start an activity or service using context.startActivity(myIntent) or 
    context.startService(myService) respectively.

⬜After we have created a BroadcastReceiver, it needs to be registered so that it can receive events (intents). 

⬜There are two ways to do this:

          ◼️Statically in the manifest file.

          ◼️Dynamically in the code.

Registering in the manifest file

⬜To declare a broadcast receiver in the manifest, we specify a <receiver> element in our app's manifest.

<receiver android:name=“.MyReceiver”  android:exported=“true”
        <action  android:name=“android.intent.action.BOOT_COMPLETED”/>      

  <action android:name=“android.intent.action.TIME_TICK” />


Registering Through Code

⬜As an alternative, we can register our BroadcastReceiver implementation dynamically in our code.

⬜This requires three steps:
         ◻️Creating a BroadcastReceiver object

         ◻️Creating an IntentFilter object

         ◻️Registering it by calling the method registerReceiver( ) of Context

⬜The registerReceiver() method takes two parameters:

BroadcastReceiver in android

MyReceiver rec=new MyReceiver( );

IntentFilter filter=new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);

registerReceiver( rc,filter);

UnRegistering Through Code

⬜To stop receiving broadcasts, we can call the method

⬜We must surely unregister the receiver when we no longer need it or the context is no longer valid.

An Important Point!

⬜We must be mindful of where we register and unregister the receiver.

⬜For example, if we register a receiver in onCreate(Bundle) using the activity's context, we should unregister it in onDestroy() to prevent leaking the receiver out of the activity context. 

⬜If we register a receiver in onResume(), we should unregister it in onPause() to prevent registering it multiple times

1 comment:

  1. your website looking cool can you senf me html code of your website