1. Overview
Now friends we are going to learn about Broadcast ReceiversBroadcast 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 file2) 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..