Reputation: 2216
I was trying to block all the incoming sms in my android device.
This is the code i was using-
public class SmsReceiver extends BroadcastReceiver {
/**
* Called when the activity is first created.
*/
private static final String ACTION = "android.provider.Telephony.SEND_SMS";
public static int MSG_TPE = 0;
@Override
public void onReceive(Context context, Intent intent) {
String MSG_TYPE = intent.getAction();
if (MSG_TYPE.equals("android.provider.Telephony.SMS_RECEIVED")) {
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, "BLOCKED Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
toast.show();
abortBroadcast();
for (int i = 0; i < 8; i++) {
Log.i("log", "Blocking SMS **********************");
}
} else if (MSG_TYPE.equals("android.provider.Telephony.SEND_SMS")) {
Toast toast = Toast.makeText(context, "SMS SENT: " + MSG_TYPE, Toast.LENGTH_LONG);
toast.show();
abortBroadcast();
for (int i = 0; i < 8; i++) {
Log.i("log", "Blocking SMS **********************");
}
} else {
Toast toast = Toast.makeText(context, "SIN ELSE: " + MSG_TYPE, Toast.LENGTH_LONG);
toast.show();
abortBroadcast();
for (int i = 0; i < 8; i++) {
Log.i("log", "Blocking SMS **********************");
}
}
}
}
Manifest file-
<service android:name=".MyServiceSentReceived" android:enabled="true"/>
<receiver android:name="SmsReceiver">
<intent-filter android:priority="2147483645">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
It shows me blocking sms but again the sms is received. so this piece of code is not working for me. i was following this SO Question.
The other questions i am looking at are -
Android Block Incoming SMS using BroadCastReceiver
Do anyone have any suggestion for this.
Upvotes: 0
Views: 1455
Reputation: 39201
You cannot block incoming SMS since API 19 (KitKat), unless your app is the default messaging app, and even then you can only stop them from being saved to the SMS Provider.
The SMS_RECEIVED
broadcast can no longer be aborted, so any app listening for it will still get it. Furthermore, the default app receives a different broadcast anyway - SMS_DELIVER
- which is not received by any other app, and cannot be intercepted.
If your app is the default SMS app, it is responsible for writing the incoming messages to the SMS Provider, so if you don't want them saved there, just don't write them. However, this will have no effect on the SMS_RECEIVED
broadcast, which will still be delivered to any app registered for it, though those apps won't be able to write them to the Provider.
The following blog page discusses the SMS API changes, and includes details on the requirements for an app to act as the default messaging app. Do note, though, that the default app is responsible for many, many things - including MMS, as well - and writing a full-blown messaging client is not a trivial task.
Getting Your SMS Apps Ready for KitKat
Upvotes: 2