0

hi I develop a android app.

and I try to do confirmition sms like in WhatsApp. but with my code after I send the SMS I can see that in my SMS app. and I wont that the user not see the sendding SMS. like in WhatApp and all this Apps

how can I do it ?

explain again I send the SMS message, but I can see that the SMS send from my phone to my phone, I want that the user see only the recived SMS message, and if it can see other number or name (in the from in the SMS)

by the way I am using this code

package app.dirot.sms;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Message;
import android.provider.Telephony;
import android.support.annotation.RequiresApi;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;

    import java.util.Random;

    import app.dirot.MainActivityMap;

    public class SmsResultReceiver extends BroadcastReceiver {
    public static final String SMS_SENT_ACTION = "com.mycompany.myapp.SMS_SENT";
    public static final String SMS_DELIVERED_ACTION = "com.mycompany.myapp.SMS_DELIVERED";
    public static final String EXTRA_NUMBER = "number";
    public static final String EXTRA_MESSAGE = "message";

    private MainActivityMap mainActivityMap;
    private SmsManager smsManager;
    private IntentFilter intentFilter;
    private int verificationNumber;

    public SmsResultReceiver(MainActivityMap mainActivityMap) {
        this.mainActivityMap = mainActivityMap;

        smsManager = SmsManager.getDefault();

        intentFilter = new IntentFilter(SMS_SENT_ACTION);
        intentFilter.addAction(SMS_DELIVERED_ACTION);
    }

    public void sendSMS() {
        mainActivityMap.registerReceiver(this, intentFilter);
        String message = "Verification message.";
        smsManager = SmsManager.getDefault();

        intentFilter = new IntentFilter(SmsResultReceiver.SMS_SENT_ACTION);
        intentFilter.addAction(SmsResultReceiver.SMS_DELIVERED_ACTION);

        this.mainActivityMap.registerReceiver(this, intentFilter);
        sendMessage(smsManager);
    }

    private void sendMessage(SmsManager smsManager) {
        // We're going to remove numbers and messages from
        // the lists as we send, so if the lists are empty, we're done.
        Random random = new Random();
        verificationNumber = random.nextInt();
        String message = "Verification message." +
                "\n code = " + verificationNumber;
        String number = "97254564546";

        // The Intents must be implicit for this example,
        // as we're registering our Receiver dynamically.
        Intent sentIntent = new Intent(SmsResultReceiver.SMS_SENT_ACTION);
        Intent deliveredIntent = new Intent(SmsResultReceiver.SMS_DELIVERED_ACTION);

        // We attach the recipient's number and message to
        // the Intents for easy retrieval in the Receiver.
        sentIntent.putExtra(SmsResultReceiver.EXTRA_NUMBER, number);
        sentIntent.putExtra(SmsResultReceiver.EXTRA_MESSAGE, message);
        deliveredIntent.putExtra(SmsResultReceiver.EXTRA_NUMBER, number);
        deliveredIntent.putExtra(SmsResultReceiver.EXTRA_MESSAGE, message);

        // Construct the PendingIntents for the results.
        // FLAG_ONE_SHOT cancels the PendingIntent after use so we
        // can safely reuse the request codes in subsequent runs.
        PendingIntent sentPI = PendingIntent.getBroadcast(this.mainActivityMap,
                1,
                sentIntent,
                PendingIntent.FLAG_ONE_SHOT);

        PendingIntent deliveredPI = PendingIntent.getBroadcast(this.mainActivityMap,
                1,
                deliveredIntent,
                PendingIntent.FLAG_ONE_SHOT);

        // Send our message.
        smsManager.sendTextMessage(number, null, message, sentPI, deliveredPI);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onReceive(Context context, Intent intent) {
        // A simple result Toast text.
        String result = null;
        mainActivityMap.unregisterReceiver(this);
        // Get the result action.
        String action = intent.getAction();

        // Retrieve the recipient's number and message.
        String number = intent.getStringExtra(EXTRA_NUMBER);
        String message = intent.getStringExtra(EXTRA_MESSAGE);

        // This is the result for a send.
        if (SMS_SENT_ACTION.equals(action)) {
            int resultCode = getResultCode();
            result = "Send result : " + translateSentResult(resultCode);
        }
        // This is the result for a delivery.
        else if (SMS_DELIVERED_ACTION.equals(action)) {
            SmsMessage sms = null;

            // A delivery result comes from the service
            // center as a simple SMS in a single PDU.
            byte[] pdu = intent.getByteArrayExtra("pdu");
            String format = intent.getStringExtra("format");

            // Construct the SmsMessage from the PDU.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && format != null) {
                sms = SmsMessage.createFromPdu(pdu, format);
            } else {
                sms = SmsMessage.createFromPdu(pdu);
            }

            // getResultCode() is not reliable for delivery results.
            // We need to get the status from the SmsMessage.
            result = "Delivery result : " + translateDeliveryStatus(sms.getStatus());
        }
        result = number + ", " + message + "\n" + result;
    }

    String translateSentResult(int resultCode) {
        switch (resultCode) {
            case Activity.RESULT_OK:
                return "Activity.RESULT_OK";
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                return "SmsManager.RESULT_ERROR_GENERIC_FAILURE";
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                return "SmsManager.RESULT_ERROR_RADIO_OFF";
            case SmsManager.RESULT_ERROR_NULL_PDU:
                return "SmsManager.RESULT_ERROR_NULL_PDU";
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                return "SmsManager.RESULT_ERROR_NO_SERVICE";
            default:
                return "Unknown error code";
        }
    }

    String translateDeliveryStatus(int status) {
        switch (status) {
            case Telephony.Sms.STATUS_COMPLETE:
                return "Sms.STATUS_COMPLETE";
            case Telephony.Sms.STATUS_FAILED:
                return "Sms.STATUS_FAILED";
            case Telephony.Sms.STATUS_PENDING:
                return "Sms.STATUS_PENDING";
            case Telephony.Sms.STATUS_NONE:
                return "Sms.STATUS_NONE";
            default:
                return "Unknown status code";
        }
    }
}
  • 1
    Since KitKat, you cannot prevent outgoing text SMS from being saved to the Provider unless your app is the default messaging app. – Mike M. Oct 22 '17 at 12:05
  • ok so how can I do confirmition sms like in WhatsApp? – shimon dadon Oct 22 '17 at 13:34
  • AFAIK, WhatsApp sends the request for a verification SMS from the app to their servers over the internet, then sends the message back to the device through a gateway. There are several third-party services available that offer similar functionality, and there are plenty of posts here that can give you suggestions. [This recent Android developers blog post](https://android-developers.googleblog.com/2017/10/effective-phone-number-verification.html?m=1) might also be of interest to you. – Mike M. Oct 22 '17 at 13:59

0 Answers0