Blog purpose for android basic example for android app developer. any query please my contact

Sunday 23 April 2023

Local BroadCast Manager in Android example

 Local BroadCast Manager in Android example



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.


1.Main Activity.java:


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

    }

}




2.Activityone.java:


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

        }

    };

}




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


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

No comments:

Post a Comment