Sometimes we have to send messages to our Android apps through SMS messages.
So we have to write a SMS receiver to receive the message. After handling the message, we can decide if we want to allow the SMS message to be displayed in the inbox of the default messaging client or not.
To acheive this, we need to do the following:
1. Add an extra permission to receive the incoming SMS message
2. Declare our SMS receiver in the AndroidManifest xml file
3. Writing a SMS receiver class to handle received messages
4. Decide if we have to showup the message in the default messaging client
Permissions needed
Adding receiver to Android Manifest
Add the sms receiver class to your manifest. If you want your app to handle the SMS message before any other app, then set the priority to the maximum: 999
Setting the maximum priority also comes with added responsibility. You can actually mess up other SMS messages that you dont want to handle from reaching the messaging app.
Add the following lines to AndroidManifest.xml
Receiver code
The MySMSReceiver class should extend the BroadcastReceiver and override the onReceive() method
Assuming the app package is com.test.myapp, add this code to MySMSReceiver.java
Now using the above code, you can intercept the SMS messages.
This will also allow the default messaging client to receive the same SMS message and display it in the Inbox.
What if you want to consume the message and not show it in the messaging app?
Just call abortBroadcast()
So we have to write a SMS receiver to receive the message. After handling the message, we can decide if we want to allow the SMS message to be displayed in the inbox of the default messaging client or not.
To acheive this, we need to do the following:
1. Add an extra permission to receive the incoming SMS message
2. Declare our SMS receiver in the AndroidManifest xml file
3. Writing a SMS receiver class to handle received messages
4. Decide if we have to showup the message in the default messaging client
Permissions needed
android.permission.RECEIVE_SMS
Adding receiver to Android Manifest
Add the sms receiver class to your manifest. If you want your app to handle the SMS message before any other app, then set the priority to the maximum: 999
Setting the maximum priority also comes with added responsibility. You can actually mess up other SMS messages that you dont want to handle from reaching the messaging app.
Add the following lines to AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<receiver android:name=".MySMSReceiver">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Receiver code
The MySMSReceiver class should extend the BroadcastReceiver and override the onReceive() method
Assuming the app package is com.test.myapp, add this code to MySMSReceiver.java
package com.test.myapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;
public class MySMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String smsData = null;
if (intent.getAction().equals(android.provider.Telephony.SMS_RECEIVED)) {
Bundle pudsBundle = intent.getExtras();
Object[] pdus = (Object[]) pudsBundle.get("pdus");
SmsMessage messages =SmsMessage.createFromPdu((byte[]) pdus[0]);
smsData = messages.getMessageBody();
// now smsData has the actual SMS message
// Add code to handle the message
}
}
}
Now using the above code, you can intercept the SMS messages.
This will also allow the default messaging client to receive the same SMS message and display it in the Inbox.
What if you want to consume the message and not show it in the messaging app?
Just call abortBroadcast()
public class MySMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String smsData = null;
boolean bHandled = false;
if (intent.getAction().equals(android.provider.Telephony.SMS_RECEIVED)) {
Bundle pudsBundle = intent.getExtras();
Object[] pdus = (Object[]) pudsBundle.get("pdus");
SmsMessage messages =SmsMessage.createFromPdu((byte[]) pdus[0]);
smsData = messages.getMessageBody();
// now smsData has the actual SMS message
// Add code to handle the message
// If this message should be aborted set bHandled = true
if (bHandled) {
abortBroadcast();
}
}
}
}
Nice Thread Tutorial, i am new in android , its help me a lot ...
ReplyDeleteI have got some good links
here at androidexample.com
Incomming SMS Broadcast Receiver
https://play.google.com/store/apps/details?id=com.sec.android.ss.updater
ReplyDeletegood tutorial......
ReplyDeleteI'm trying to develop an app by which it receives the incoming message and block it from continuing into the main message application. I've tried so many codes and didn't work. Any help please?
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteI can intercept WhatsApp, Viber, SMS, Calls, Phone Book etc with SpyToMobile. This app is cheaper than others. If you want to find out if somebody is cheating on you or not - Welcome to SpyToMobile :)
ReplyDeletehttp://spytomobile.com
Nice article really informative thank you for sharing and please share more in future content like this android sms sender
ReplyDelete