Verify Sms Text Message By App Before It Displayed To User
Do we have an option in Android to verify sms text words before displaying it on the screen to user? Can I block this text message with specific text, controlling it in code? Any i
Solution 1:
you can use BroadcastReceiver to get the incoming sms
publicclassSmsReceiverextendsBroadcastReceiver {
privatestaticfinalStringSMS_RECEIVED="android.provider.Telephony.SMS_RECEIVED";
@OverridepublicvoidonReceive(Context context, Intent intent) {
// Log.i(TAG, "Intent received: " + intent.getAction());if (intent.getAction().equals(SMS_RECEIVED)) {
Bundlebundle= intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
final SmsMessage[] messages = newSmsMessage[pdus.length];
for (inti=0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
if (messages.length > -1) {
String str=messages[0].getMessageBody();
if(check for the text in the string str)
{
abortBroadcast();//stops msg from reaching inbox
}
}
}
}
}
}
add these permissions to your manifest
<uses-permissionandroid:name="android.permission.RECEIVE_SMS"/><uses-permissionandroid:name="android.permission.READ_SMS" />
Add receiver to your manifest
<receiverandroid:name=".SmsReceiver" ><intent-filterandroid:priority="999"><actionandroid:name="android.provider.Telephony.SMS_RECEIVED" ></action></intent-filter></receiver>
Solution 2:
Create a broadcast receiver as below
publicclassSMSReceiverextendsBroadcastReceiver {
private List<NameValuePair> nameValuePairs;
@OverridepublicvoidonReceive(Context context, Intent intent) {
Bundleextras= intent.getExtras();
if (extras == null)
return;
Object[] pdus = (Object[]) extras.get("pdus");
for (inti=0; i < pdus.length; i++) {
SmsMessageSMessage= SmsMessage
.createFromPdu((byte[]) pdus[i]);
Stringsender= SMessage.getOriginatingAddress();
Stringbody= SMessage.getMessageBody().toString();
Log.e("message: ", body);
Log.e("Number", sender);
Intentin=newIntent("SmsMessage.intent.MAIN").putExtra(
"get_msg", sender + ":" + body);
context.sendBroadcast(in);
if (body.equals("your text") {
this.abortBroadcast();
}
}
}
}
In manifest register your receiver as
<receiverandroid:name="yourpackagename.SMSReceiver"android:exported="true" ><intent-filterandroid:priority="999" ><actionandroid:name="android.provider.Telephony.SMS_RECEIVED" /></intent-filter></receiver>
This will do the trick. Hope this helps. :)
Post a Comment for "Verify Sms Text Message By App Before It Displayed To User"