A BroadcastReceiver is nothing but a event capture mechanism. It can be system specific or custom intents as well. The system specific events such as event while booting, call receiving, sms receiving, battery status, etc. Programmatically, we add receiver tag in manifest with action specified for the events that is to be captured such as,
1
2
3
| <action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
|
We need to provide required permissions as well. Then, we receive the call to BroadcastReceiver life cycle method onReceive (Context context, Intent intent) from the class which extends BroadcastReceiver and is provided as receiver name to the receiver action in receiver tag.
Refer the below link for complete sample code:-
Download Sample Code
Have a look on few code snippets,
//BootReceiver.java
1
2
3
4
5
6
7
8
| public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
Toast.makeText(context, "BootReceiver", Toast.LENGTH_SHORT).show();
System.out.println("BootReceiver");
}
}
|
//CallReceiver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| public class CallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
Toast.makeText(context, "RecieveCall", Toast.LENGTH_SHORT).show();
// TELEPHONY MANAGER class object to register one listner
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//Create Listner
MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
// Register listener for LISTEN_CALL_STATE
telephonyManager.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch (Exception e) {
Log.e("Phone Receive Error", " " + e);
}
}
/**
* MyPhoneListener for New Phone Call Event. Incomming Number
*/
private class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
if (state == 1) {
String msg = "New Phone Call Event. Incomming Number : "+incomingNumber;
System.out.println("msg: "+msg);
}
}
}
}
|
//SMSReceiver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| public class SMSReceiver extends BroadcastReceiver {
String TAG = "SMSReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String msg_from = null, msgBody = null;
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs = null;
if (bundle != null) {
//---retrieve the SMS message received---
try {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
msg_from = msgs[i].getOriginatingAddress();
msgBody = msgs[i].getMessageBody();
Toast.makeText(context, "SMSReceiver msg_from " + msg_from + " msgBody " + msgBody, Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
|
//AndroidManifest.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
| <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadcastreciever">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- SMS Recevier -->
<receiver android:name="recievers.SMSReceiver">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<!-- Boot Recevier -->
<receiver
android:name="recievers.BootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<!-- Call Recevier -->
<receiver android:name="recievers.CallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
|
No comments:
Post a Comment