You are on page 1of 9

Local BroadcastManager connect to Android App

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.

Advantage of using Local BroadcastManager

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

Purchased by Guilllermo Ortiz, guillermo.ortizz@hotmail.com #7327154

Start App
Launch 1st Activity

Register Receiver

User clicks Second


Activity

Launch 2nd Activity


Enter message
and click send
Receives receivers message

User clicks back button

Launches 1st Activity


Update message field
to show users message

On exit unregister receiver


Stop
Figure 1: Flowchart of Local BroadcastManager

Implementation of Local Broadcast Manager to Android


App
Following is the step-by-step instructions to implement integration of File Download to Android app:
1. Launch the Android Studio.
2. On File menu, click New Project. The New Project dialog box appears.

Purchased by Guilllermo Ortiz, guillermo.ortizz@hotmail.com #7327154

Figure 2: New Project dialog box

3. In New Project dialog box, perform the following:


3.1 Enter the application name in Application name box.
3.2 Enter the company domain in Company Domain box.
3.3 Select or enter the project location in Project Location box.
3.4 Click Next to proceed further. The Target Android Devices dialog box appears.

Figure 3: Target Android Devices dialog box

4. In Target Android Devices dialog box, perform the following:


4.1 Check Phone and Tablet.
4.2 Select the required minimum SDK from Minimum SDK drop down.
4.3 Click Next to proceed further. The Add an activity to Mobile dialog box appears.

Purchased by Guilllermo Ortiz, guillermo.ortizz@hotmail.com #7327154

Figure 4: Add an activity to Mobile dialog box

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.

Figure 5: Customize the Activity dialog box

6. In Customize the Activity dialog box, perform the following:


6.1 Enter activity name in Activity Name box.
6.2 Enter layout name in Layout Name box.
6.3 Enter title in Title box.
6.4 Enter menu resource name in Menu Resource Name box.
6.5 Click Finish to complete the creation of new project.
7. Navigate to app\src\main\AndroidManifest.xml in Android Project section and add the
following:
Code
<application

Purchased by Guilllermo Ortiz, guillermo.ortizz@hotmail.com #7327154

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"

Purchased by Guilllermo Ortiz, guillermo.ortizz@hotmail.com #7327154

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.

Figure 6: Preview of activity_main.xml

9. Navigate to app\src\main\res\layout\activity_second.xml and add the following:


Code
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Message to previous Activity"
android:id="@+id/button2"
android:layout_centerInParent="true" />

Purchased by Guilllermo Ortiz, guillermo.ortizz@hotmail.com #7327154

<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.

Figure 7: Preview of activity_second.xml

10. Navigate to app\src\main\java\com\localbroadcastmanager and add the following:


Code
public class MainActivity extends Activity {

Purchased by Guilllermo Ortiz, guillermo.ortizz@hotmail.com #7327154

TextView message;
Button secondActivity;

public static final String INTENT_NAME =


"com.sebi.localbroadcastmanger.INTENT";
//Recieving Broadcast
private BroadcastReceiver myBroadcastReceiver = new
BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(MainActivity.this, "Recieved
Message", Toast.LENGTH_SHORT).show();
message.setText("Recieved
Message:"+intent.getStringExtra("msg"));
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

message = (TextView) findViewById(R.id.textView);


secondActivity = (Button) findViewById(R.id.button);

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));

Purchased by Guilllermo Ortiz, guillermo.ortizz@hotmail.com #7327154

}
});
}

@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.

Purchased by Guilllermo Ortiz, guillermo.ortizz@hotmail.com #7327154