1

I added a phone verification sms feature to the registration process of my app but the sms is written as sent and received in the SMS inbox. At the moment I only send a simple text just to verify the number so nothing secret here but it's not so user friendly so I was wondering if I could somehow prevents this SMS to be written.

I added abortBroadcast(); if the code is correct but it had no effect. I'm using Android 4.4.2 and I've read that for security reasons, there is no way to do that so is there a way to achieve what I want maybe in a different way? I know other apps do manage to do so...

The code for sending SMS:

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(Utils.phone, null, "text", null, null);

The code for consuming thr SMS:

public class SMSReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        Object[] pdus = (Object[]) bundle.get("pdus");
        SmsMessage messages = SmsMessage.createFromPdu((byte[]) pdus[0]);
        if (messages.getMessageBody().contains("text")) {
            Toast.makeText(Utils.context,"ok",Toast.LENGTH_LONG).show();
            abortBroadcast();
        }
    }
}

This is the manifest:

<receiver android:name="com.tomerapp.tomerapp.utils.SMSReceiver" android:exported="false">
    <intent-filter android:priority="999">
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
    </intent-filter>
</receiver>

Does android:exported="false" means the receiver/my app will only catch SMSs sent from the app itself? I don't want to waste battery for SMSs other than what the app sends.

Amos
  • 1,321
  • 2
  • 23
  • 44
  • Starting with Android 4.4, only the default SMS app has standard write access to the Provider, and non-default apps don't normally have any control over messages being written or deleted. If your app is just sending a single verification message, I wouldn't worry about trying to delete it. It's probably more trouble than it's worth. – Mike M. Mar 08 '15 at 20:11
  • whatsapp confirms the same way, the sms didn't appear in the list – Amos Mar 08 '15 at 20:27
  • I'm not sure how WhatsApp does it, but you could have the app send itself a data message, and it won't show in the native app. Also, if WhatsApp is selected as the default SMS app at the time of the verification, it can opt not to write the message to the Provider, so it won't show in "the list". – Mike M. Mar 08 '15 at 20:36
  • please explain "the app send itself a data message," – Amos Mar 08 '15 at 20:45
  • Check this post: http://stackoverflow.com/questions/3757229/how-to-send-and-receive-data-sms-messages – Mike M. Mar 08 '15 at 20:48
  • 1
    Please add the data sms as an answer so I can accept it – Amos Mar 08 '15 at 21:57

1 Answers1

1

Starting with Android 4.4 (KitKat), only the app selected as the default SMS app has standard write access to the Provider. If your app is the default, it is responsible for writing all incoming and its own outgoing messages, and it can opt not to do so.

Another possibility is to use data messages, as these aren't handled by the Provider, and therefore won't show in other SMS apps. This post demonstrates how to send data messages, and how to configure a BroadcastReceiver to look for them.

Community
  • 1
  • 1
Mike M.
  • 38,532
  • 8
  • 99
  • 95