Professional Documents
Culture Documents
Overview
The Local BroadcastManager is used to send and receive local Broadcast. In order to send
data/intent application-wide, Local Broadcast is required to be used.
The Local Broadcast Manager is introduced with an intent to Android Support library to ease the
process of registering to send and receive intents between components within your app. The intent
broadcast cannot be received by any other components outside the app. Likewise, other application
cannot transmit broadcasts to your receiver.
Broadcast data will not leave the app. So no leaking of private data
More efficient than sending a global broadcast
No security holes. Since it is impossible for other applications to send these broadcasts to
your app
Basic Flowchart
Start App
Launch 1st Activity
Register Receiver
5. In Add an activity to Mobile dialog box, select the required type of activity from list of
activities. And click Next to proceed further. The Customize the Activity dialog box appears.
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/title_activity_second" >
</activity>
</application>
Table 1: AndroidManifest.xml file
Description
The AndroidManifest file has two activities. The 1st activity is the launcher activity. And second
activity is used to broadcast messages.
8. Navigate to app\src\main\res\layout\activity_main.xml and add the following:
Code
<TextView android:text="If any message is recieved it will be
shown here." android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to second Activity"
android:id="@+id/button"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"/>
Table 2: activity_main.xml
Description
This is simple layout which has a RelativeLayout as the base layout. The TextView and Buttons
as its child views. The base layout of core activity is the MainActivity. The topmost layout is a
RelativeLayout.
The layout contains the following:
A Textview field with text set to If any message is received it will be shown here. The
width and height set to wrap content. The id of the Textview is set to textView.
The button height and width is set to wrap content. The text of the button is set to Go
to second Activity. The id is set to button. The button is placed centre horizontal.
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_above="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp" />
Table 3: activity_second.xml file
Description
This is simple layout which has a RelativeLayout as the base layout. The EditText and Buttons as
its child views. The topmost layout is a RelativeLayout.
The layout contains the following:
The width of the EditText is set to match parent and height set to wrap content. The id
of EditText set to editText. The EditText is placed centre horizontal and above button.
The width and height of the button is set to wrap content. The text of the button is set
to Send Message to previous Activity. The id of the button is set to button2 and placed
at the centre.
TextView message;
Button secondActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocalBroadcastManager.getInstance(this).registerReceiver(myBro
adcastReceiver, new IntentFilter(INTENT_NAME));
secondActivity.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,
SecondActivity.class));
}
});
}
@Override
protected void onDestroy(){
super.onDestroy();
LocalBroadcastManager.getInstance(this).unregisterReceiver(myBroadcastReceiver);
}
Table 4: MainActivity.java file
Description
In the MainActivity, the references to EditText and Button is declared. The Broadcast Receiver with
the intent filter com.sebi.localbroadcastmanager.INTENT is registered. The onClick listener
navigates the user to second activity on click of the button. The onDestroy method unregisters the
Broadcast Receiver. The onReceive method receives the Broadcast message sent by second activity
and displays the text in TextView.
In second activity, on Create method references are made to EditText and Button. In onClick
listener, the message entered in EditText is fetched and Broadcast message is set with intent filter
com.sebi.localbroadcastmanager.INTENT.