发送和接收短信验证手机号码 [英] send and receive sms to verify mobile number

查看:200
本文介绍了发送和接收短信验证手机号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做的手机号码验证,而无需使用第三方。对于这个我的逻辑是这样的: -

I am trying to do mobile number verification without using third party. For this my logic is this:-


  • 用户输入自己的手机号码与国家code

  • 当他们点击确认按钮意图将短信发送到用户定义的手机号码随机唯一的ID

  • 该应用播出后会等待2分钟,当它接收短信则用户可以登录或注册

这是逻辑正确,或者需要一些修改?

Is this logic right or it need some modification?

要送我用这code短信,但不知道我怎么能接受和验证号码。

To send sms i am using this code, but dont know how i can recieve and validate number.

 String phoneNumber = "9999999999";
 String smsBody = "Message from the API";

 // Get the default instance of SmsManager
 SmsManager smsManager = SmsManager.getDefault();
 // Send a text based SMS
 smsManager.sendTextMessage(phoneNumber, null, smsBody, null, null);

更新: -

如果没有收到三星设备上的信息。

Not receiving message on samsung device.

<一个href=\"http://stackoverflow.com/questions/31224640/this-requires-android-permission-interact-across-users-full\">this需要android.permission.INTERACT_ACROSS_USERS_FULL

推荐答案

首先,我产生范围内的10000的随机数到99999。

First I generate a random number within range 10000 to 99999.

 Random rNo = new Random();
 final int code = rNo.nextInt((99999 - 10000) + 1) + 10000;

接着我显示一个弹出提的一个消息从移动被发送到验证号码用户。这是使用 AlertDialog 完成的。

final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Verify Phone Number");
builder.setMessage("An sms will be sent to the number " + phNo + " for verification. Charges will apply as per your plan");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(final DialogInterface dialog, int which) {
                //code to send sms here with the code value
                final ProgressDialog progressdialog = ProgressDialog.show(getActivity(), "Waiting for SMS", "Please hold on");

                final CountDownTimer timer = new CountDownTimer(120000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                    Log.v("ranjapp", "Ticking " + millisUntilFinished / 1000);
                     progressdialog.setMessage("Waiting for the message " + millisUntilFinished / 1000);
                    }

                    @Override
                    public void onFinish() {
                     getActivity().unregisterReceiver(receiver);
                     progressdialog.dismiss();

                    }
                }.start();

                receiver = new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        Bundle bundle = intent.getExtras();
                        if (bundle != null) {
                            if (readSMS(intent, code)) {
                                Log.v("ranjapp", "SMS read");
                                timer.cancel();
                                try {
                                    getActivity().unregisterReceiver(receiver);
                                } catch (Exception e) {
                                }
                            }
                        }
                    }
                };
     getActivity().registerReceiver(receiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
            }
        }

);
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener()

        {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }

);
builder.show();

以上code的说明:

用户确认后发送SMS短信与code到输入的号码发送,这个数字应该是用户使用的是自己的人。因此,我们将等待与code短信,接收短信和阅读其内容,我们将创建一个广播接收器收听来电短信。

After user acknowledges to send sms an sms is sent with the code to the entered number and this number should be the one that user is using himself. So we will wait for an sms with the code, to receive the sms and read its content we will create a BroadCastReceiver to listen to incoming sms.

现在我们也需要启动一个定时器,使得我们等待只有2分钟,短信,所以我们开始 CountDownTimer 在2分钟结束2分钟该countdowntimer将注销接收器。接收器是未注册的,即使code在收到SMS,这样我们可以免费的资源。

Now also we need to start a timer so that we wait for only 2 minutes for the sms, so we start a CountDownTimer for 2 minutes at the end of 2 minutes the countdowntimer will unregister the receiver. The receiver is unregistered even if the code is received in the incoming sms so that we can free the resources.

code到readSMS,如果code为找到返回true否则返回false

Code to readSMS, if the code is found true is returned else false is returned,

boolean readSMS(Intent intent, int code) {
        try {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                Object[] pdusObj = (Object[]) bundle.get("pdus");
                for (int i = 0; i < pdusObj.length; i++) {
                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();
                    if (message.contains(String.valueOf(code)))
                        return true;
                }
            }
        } catch (Exception e) {
            Log.v("ranjapp", "Exception here " + e.toString());
            return false;
        }
        return false;
    }

要低于发送短信的方法使用:

To send SMS use below method:

public static void sendSMS(Context context, String incomingNumber, String sms) {
        DateTimeFormatter dtfOut = DateTimeFormat.forPattern("YYYY-MM-dd HH:MM:SS");
        SmsManager smsManager = SmsManager.getDefault();                                      //send sms
        try {
            ArrayList<String> parts = smsManager.divideMessage(sms);
            smsManager.sendMultipartTextMessage(incomingNumber, null, parts, null, null);

            RecContDBHelper recContDBHelper = new RecContDBHelper(context);
            recContDBHelper.insertRecord(new ContactData("", incomingNumber, dtfOut.print(MutableDateTime.now())));
            Log.v("ranjith", "Sms to be sent is " + sms);
        } catch (Exception e) {
            Log.v("ranjith", e + "");
        }
    }

在AndroidManifest.xml中,你需要拥有这些权限:

In AndroidManifest.xml you need to have these permissions:

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />

这篇关于发送和接收短信验证手机号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆