发送确认短信android没有看到发送消息

时间:2017-10-22 12:00:03

标签: java android broadcastreceiver sms

嗨,我开发了一个Android应用程序。

我尝试在WhatsApp中做确认短信。 但是在我发送短信后我的代码可以在我的短信应用中看到。我不认为用户没有看到发送短信。比如WhatApp和所有这些应用程序

我该怎么办?

再次解释我发送短信,但我可以看到短信从我的手机发送到我的手机,我希望用户只能看到收到的短信息,如果能看到其他号码或姓名(在来自SMS)

顺便说一下我使用这段代码

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";
        }
    }
}

0 个答案:

没有答案