The Local Broadcast Manager was introduced to the Android Support Library to simplify the process of registering for, and sending, Broadcast Intents between components within your application.
Because of the reduced broadcast scope, using the Local Broadcast Manager is more efficient than sending a global broadcast. It also ensures that the Intent you broadcast cannot be received by any components outside your application, ensuring that there is no risk of leaking private or sensitive data, such as location information.
Similarly, other applications can’t transmit broadcasts to your Receivers, negating the risk of these Receivers becoming vectors for security exploits.
To use the Local Broadcast Manager, you must first include the Android Support Library in your application, in order to do so open your sdk and locate Extras and click to download package Android Support Library.
Main Activity
package com.example.localbroadcastmanager; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.support.v4.content.LocalBroadcastManager; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button b1 = (Button) findViewById(R.id.button1); b1.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View v) { // TODO Auto-generated method stub sendBroadcast(); startActivity(new Intent("android.intent.action.Activityone")); } public void sendBroadcast() { Intent intent = new Intent("send"); intent.putExtra("current speed", "102.4"); intent.putExtra("latitude", "12.2342342"); intent.putExtra("longitude", "12.21124"); LocalBroadcastManager.getInstance(this).sendBroadcast(intent); } }
ActivityOne
package com.example.localbroadcastmanager; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.support.v4.content.LocalBroadcastManager; import android.widget.TextView; public class Activityone extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.layoutone); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); LocalBroadcastManager.getInstance(this).unregisterReceiver(message); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); LocalBroadcastManager.getInstance(this).registerReceiver(message, new IntentFilter("send")); } private BroadcastReceiver message = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Double speed_received = intent.getDoubleExtra("current speed", 10); Double latittude_received = intent.getDoubleExtra("latitude", 0); Double longitude_received = intent.getDoubleExtra("longitude",0); TextView speed = (TextView)findViewById(R.id.textView1); speed.setText("speed : "+ speed_received); TextView latitude = (TextView)findViewById(R.id.textView2); latitude.setText("latitude : "+ latittude_received); TextView longitude = (TextView)findViewById(R.id.textView3); longitude.setText("longitude : "+ longitude_received); } }; }
Activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:text="BroadCast" /> </RelativeLayout>
LayoutOne.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="30dp" android:text="TextView" android:layout_marginTop="30dp" android:textSize="20dp" /> <TextView android:id="@+id/textView2" android:layout_width="fill_parent" android:layout_height="30dp" android:text="TextView" android:layout_marginTop="50dp" android:textSize="20dp" /> <TextView android:id="@+id/textView3" android:layout_width="fill_parent" android:layout_height="30dp" android:text="TextView" android:layout_marginTop="70dp" android:textSize="20dp" /> </LinearLayout>