Creating Broadcast receiver
Android Broadcastreceiver is the android component where you can register for
system level or Application level events.these events you will be notified after registering.
Broadcastreceiver originating from two aspects.
- System level. eg. Battery Low Notifications.
- Application level. eg. Application level is like when you download mp3 file, mp3 player gets notified about it and gets added to player list. Other
examples of Broadcast Receiver from applications are new friend notifications,
new friend feeds, new message etc.
Notifications like incoming
messages, wi-fi, and Bluetooth activation signal, new wi-fi in the range, low
battery signal are the real time examples of the BroadcastReceiver. Thus
BroadcastReceiver are everywhere in the android system.
There are two ways to register android broadcastreceivers.
- static way in which the broadcastreceiver is registered in an android application via AndroidManifest.xml . Use
<receiver> tag in your Manifest files (AndroidManifest.xml).
- Another way of registering the broadcast receiver is dynamic, which is
done using Context.registerReceiver() method. Dynamically registered
broadcast receivers can be unregistered using
Context.unregisterReceiver() method.
Block Diagram of the system:
Implementation:
A broadcast receiver extends BroadcastReceiver abstract class. Which means that we have to implement the onReceive() method of this
base class. Whenever the event occurs Android calls the onReceive()
method on the registered broadcast receiver. For example, if you register for ACTION_POWER_CONNECTED event then
whenever power got connected to the device, your broadcast receiver’s
onReceive() method will be invoked.
Main.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class Main extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void broadcastCustomIntent(View view)
{
Intent intent = new Intent("MyCustomIntent");
EditText et = (EditText)findViewById(R.id.extraIntent);
// add data to the Intent
intent.putExtra("message", (CharSequence)et.getText().toString());
intent.setAction("com.broadcastreceiver.A_CUSTOM_INTENT");
sendBroadcast(intent);
}
}
In above main activity class we broadcast an event with input message from user.
intent.setAction() methode can call this broadcast intent.
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" >
<EditText android:id="@+id/extraIntent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/sendMessage" />
<Button
android:id="@+id/btnStartBroadcast"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/extraIntent"
android:onClick="broadcastCustomIntent"
android:text="@string/myBroadcastIntent" />
</RelativeLayout>
Above code snippet is for Material design for getting input from user with button.
button having onClick methode to performe the event.
MyBroadcastReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// Extract data included in the Intent
CharSequence intentData = intent.getCharSequenceExtra("message");
Toast.makeText(context, "received the Intent's message: "+intentData, Toast.LENGTH_LONG).show();
}
}
from AndroidManifest file we get receiver class to receive event.so above MyBroadcastReceiver.java class having onReceive(Context context, Intent intent).
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadcastreceiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.broadcastreceiver.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.broadcastreceiver.MyBroadcastReceiver" >
<intent-filter>
<action android:name="com.broadcastreceiver.A_CUSTOM_INTENT" >
</action>
</intent-filter>
</receiver>
</application>
</manifest>
Above AndroidMenifest.xml file contens receiver class registeration and Intent filter.
Final output.
stay tune for next session happy coding :)