Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Broadcast Receivers

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 172
    Comment on it

    Broadcast Receivers are used to respond to the broadcast messages that are generated from application or from system.

    The messages/ broadcasts that  are caught or responded by our Broadcast receivers are sometimes called intents or events.

     

    There are two types of Broadcasting Messages:-
    1) System generated Broadcasting Message.
    2) Own Application generated Broadcasting Message.

     

    In My example there are two receivers that seperately makes you clear between system generated and application generated Broadcast

    Step 1:- Generate intents/broadcast messages  by our application as:-

    Intent intent_ = new Intent();
    intent_.setAction("CUSTOMACTION");
    intent_.putExtra("This is my own BroadCasting Message");
    sendBroadcast(intent_);


    Step 2:- Create the Receiver :-

    RECEIVER ONE to receive custom msg:-

    public class MyBroadcastReceiverOne extends BroadcastReceiver {
        public static final String ACTION_RESP ="ActionResponse";
    
        @Override
        public void onReceive(Context context, Intent intent) {
            String text = intent.getStringExtra("Out Msg");
            System.out.println(" MSG "+ " On BroadCast Receiver class "+text);
           Toast.makeText(context, text+"Service on received", Toast.LENGTH_SHORT).show();
        }
    }
    

    RECEIVER Two to receive system broadcasts:-

    public class MyBroadcastReceiverTwo extends BroadcastReceiver {
        public static final String ACTION_RESP ="ActionResponse";
    
        @Override
        public void onReceive(Context context, Intent intent) {
           Toast.makeText(context, "The date has been Changed", Toast.LENGTH_SHORT).show();
        }
    }

     

    Step 3:- Register bothe the Receiver:-

    You need to register  broadcast receiver in your manifest , but make sure you must define the "action" attribute  which  clarifies  the broadcast/message/event that is needed to be handle by this Broadcast receiver.

    Note:-

    1) The receiver One handle the intent whose action is set as "CUSTOMACTION" as we have set in our above intent and perform the task as directed in the receiver

    2) Receiver Two shows the toast when you change the date of your device , system fire a intent or event with action "android.intent.action.DATE_CHANGED" and as this event is defined in the manifest declaration of receiver two

    <receiver android:name=".MyBroadcastReceiverOne">
        <intent-filter>
            <action android:name="CUSTOMACTION"/>
        </intent-filter>
    </receiver>
    
    <receiver android:name=".MyBroadcastReceiverTwo">
    <action android:name="android.intent.action.DATE_CHANGED"/>
    </intent-filter> 
    </receiver>
    
    

     

    You can also register the intent in your activity also, Here Intent Filter is used to set the actiontype that we are doing in Manifest  under the "action" attribute, by this we donot need to register in Manifest . But  registration in Manifest is more recommendable.

    IntentFilter filter = new IntentFilter("CUSTOMACTION");
     receiver = new MyBroadcastReceiver();
     registerReceiver(receiver, filter);

     





     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: