Broadcast Receiver is one of the main components of
the Android operating system. Broad cast receivers respond to broad cast messages from other applications or from the operating system itself. The Android operating system will broad cast messages to all the registered receivers whenever an event occurs in the system. Some of the Broadcast the Android system has is as follows.
- android.intent.action.BATTERY_CHANGED
- android.intent.action.BATTERY_LOW
- android.intent.action.BOOT_COMPLETED
- android.intent.action.CALL
- etc
These are the built-in Broadcasts, and if any of the receivers are registered for these events then the system will send broadcast messages to all the receivers at the time the event happens. Here, I am going to describe how to create custom broadcast receivers. In this application, I have a button, on clicking that button a custom broadcast will be triggered and I will have a broadcast listener, this listener will catch this event and will show a Toast. For this first create the main layout for the activity like the following.
- <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:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
-
-
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:id="@+id/broadCastButton"
- android:text="Send BroadCast"/>
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:text="Broad Cast Sample"
- android:id="@+id/textView"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true" />
- </RelativeLayout>
Now bind the button for the java activity in the onCreate(),
- Button button = (Button) findViewById(R.id.broadCastButton);
Now create a class which extends from the BroadCastListner like the following. This class will be called in response for the broadcast message fired.
- public class MyBroadCastListner extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- Toast.makeText(context,"Broad cast received",Toast.LENGTH_SHORT).show();
- }
- }
Now go to the manifest file and register our receiver there with the action name(this action name is custom).
- <receiver android:name=".MyBroadCastListner">
- <intent-filter>
- <action android:name="com.negociosit.CUSTOM_INTENT"></action>
- </intent-filter>
- </receiver>
Now this receiver is registered. Now we need to fire this broadcast; for that, implement the button click listener and do the code for broad cast like calling an Activity, the difference is here we are specifying the action name which we were given in our manifest and invoking it as sendBroadcast().
- button.setOnClickListener(new View.OnClickListener() {
-
- @Override
-
- public void onClick(View view) {
-
- Intent intent = new Intent();
-
- intent.setAction("com.negociosit.CUSTOM_INTENT");
-
- sendBroadcast(intent);
-
- }
-
- });