What is BroadCast Receiver in android ?
Ans-
Only an Intent (for which it is registered) can bring it into
action. The Broadcast Receiver’s job is to pass a notification to
the user, in case a specific event occurs.
Using
a Broadcast Receiver, applications can register for a particular
event. Once the event occurs, the system will notify all the
registered applications.
There
are Two Way to Register BroadCast Receiver --
- Static : Use <receiver> tag and register in menifest file
- Dynamic : Context.registerReceiver() to add dynamically .
There
are two main Classes Of BroadCast --
- Normal BroadCast- the registered receivers often run all at the same time. This is very efficient, but the Receivers are unable to utilize the results.
- Ordered BroadCast - These broadcasts are synchronous, and therefore follow a specific order. The order is defined using android: priority attribute.
Difference
between Activity Intent and Broadcast Intent --
The
intent used to start an Activity makes changes to an operation the
user is interacting with, so the user is aware of the process.
However, in case of broadcasting intent, the operation runs
completely in the background, and is therefore invisible to the user.
Create
A BroadCast Recevier with following steps--
- Creating Sub Class of BroadCast Recevier
- Overriding onReceiver() method of BraodCast Receiver class with following arguments-
- Context (Indicate reference to your application)
- Intent
Sercurity
in BroadCast Receiver
As
the broadcast receivers have a global work-space, security is very
important concern here. If you do not define the limitations and
filters for the registered receivers, other applications can abuse
them.
Here
are a few limitations that might help:
- Whenever you publish a receiver in your application’s manifest, make it unavailable to external applications by using android: exported=”false”. You might think that specifying Intent filters.
- Similarly, when you register your receiver using registerReceiver, any application may send it broadcasts. This can be prevented using permissions as well.
- When you send a broadcast, it is possible for the external applications too to receive them. This can be prevented by specifying a few limitations.
Example
of BroadCast Receiver in android
Receiving
SMS using BroadCast Receiver with the following steps --
- Create a Sub Class of Braodcast Receiver with the follow code which will receiver the SMS
package
com.example.sms_receiving;
import
android.content.*;
import
android.os.Bundle;
import
android.telephony.*;
import
android.widget.Toast;
public
class SMSReceiver extends BroadcastReceiver {
@Override
public
void onReceive(Context context, Intent intent) {
Bundle
bundle = intent.getExtras();
Object
messages[] = (Object[]) bundle.get("pdus");
SmsMessage
smsMessage[] = new SmsMessage[messages.length];
for
(int n = 0;n< messages.length; n++) {
smsMessage[n]
= SmsMessage.createFromPdu((byte[]) messages[n]);
}
//
show first message
Toast
toast = Toast.makeText(context,
"Received
SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
toast.show();
}
}
2
- Now Register your Receiver in android menifest file as follows-
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sms_receiving"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="15"
/>
<uses-permission
android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission
android:name="android.permission.RECEIVE_SMS">
</uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
/**
This is the Receiver registration in android menifest **/
<receiver
android:name=".SMSReceiver">
</receiver>
/**
----------------------------------------------------------**/
</application>
</manifest>
Now
when ever Receiver will receiver the message then it will Toast the
message to the user screen .
Some
Important Topic Of Android With There Practical Example
In the beginning, I would like to thank you much about this great post. Its very useful and helpful for anyone looking for tips. I like your writing style and I hope you will keep doing this good working.
ReplyDeleteAndroid Training Institute in Chennai | Android Training Institute in anna nagar | Android Training Institute in omr | Android Training Institute in porur | Android Training Institute in tambaram | Android Training Institute in velachery