Android Broadcast Receivers


1. Overview 

Now friends we are going to learn about  Broadcast Receivers
Broadcast Receivers is an android componant which response to broadcast messages from an applications or system itself.Broadcast Receivers will catch specific event from android oprating system, this is happen when wifi on,battery status change,recieved phone calls and cut etc.we can also send Broadcast messages throw intent with the help of "sendBroadcast(intent object)" methos.

there are two way to create Broadcast Receiver

1) static type - registered receiver at AndroidManifest.xml file
2) dynamic type -  registered receiver at dynamically with the help of registered() or unregisterReceiver() methods


2.. How to registered receiver at AndroidManifest.xml file

MyReceiver is a name of Broadcast Receiver

<receiver android:name="MyReceiver">
       <intent-filter>
          <action android:name="com.hello.app"/>
          <category android:name="android.intent.category.DEFAULT"/>
       </intent-filter>
</receiver>

3. Create activity_main.xml layout

In this layout we will take two componant first EditText and second Button.we wiil get some text from EditText and after click on Button send broadcast message

<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" >
   <EditText
      android:id="@+id/editText1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true"
      android:layout_marginLeft="44dp"
      android:layout_marginTop="26dp"
      android:ems="10" >
    <requestFocus />
  </EditText>
  <Button 
   android:id="@+id/button1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignLeft="@+id/editText1"
   android:layout_below="@+id/editText1"
   android:layout_marginTop="32dp"
   android:text="Send Signal" />
</RelativeLayout>


4.How to create  Broadcast  sender

send Broadcast message by sendBriadcast method

package com.example.broadcastsenderapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {

EditText et;
Button bt;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
          et = (EditText) findViewById(R.id.editText1);
          bt = (Button) findViewById(R.id.button1);
       bt.setOnClickListener(new OnClickListener()
       {
            @Override
            public void onClick(View v)
            {
                   String data = et.getText().toString();
                   Intent in = new Intent("com.hello.app");
                   in.putExtra("data",data);
                   sendBroadcast(in);
            }
       });
    }
    @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;
       }
  }

3.Create Broadcast Receiver

here  receive message by intent

package com.example.broadcastsenderapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context arg0, Intent in)
   {
        String data = in.getStringExtra("data");
        Toast.makeText(arg0, data, 2).show();
    }
}


if you have any issue and want to know more about Broadcast receiver please comments on blow comments box.

Thanks.. 

10 comments

its very usefull
thanks manish

Reply
This comment has been removed by the author.

Grab the Digital Marketing Training in Chennai from Infycle Technologies, the best software training institute, and Placement center in Chennai which is providing professional software courses such as Data Science, Artificial Intelligence, Cyber Security, Big Data, Java, Hadoop, Selenium, Android, and iOS Development, DevOps, Oracle, etc with 100% hands-on practical training. Dial 7504633633 to get more info and a free demo and to grab the certification for having a peak rise in your career.

Reply

Finish the Sselenium Training in Chennai from Infycle Technologies, the best software training institute in Chennai which is providing professional software courses such as Data Science, Artificial Intelligence, Java, Hadoop, Big Data, Android, and iOS Development, Oracle, etc with 100% hands-on practical training. Dial 7502633633 to get more info and a free demo and to grab the certification for having a peak rise in your career.

Reply

Free Windows 7 Product Key 2022 · CTPG3-H28YT-JP7WD-TTJ3T-RYHKG · RGQ3V-MCMTC-6HP8R-98CDK-VP3FM · MVYTY-QP8R7-6G6WG-87MGT-CRH2P · MM7DF-.Windows 7 Professional Product Key Free

Reply

The basic ingredients for Merry and Happy Christmas are gifts of time and love. I am giving you all my love so that you have the best holiday . Merry Christmas to My Wife

Reply