使用SMS验证设备的电话号码 [英] Using SMS to verify a device's phone number

查看:148
本文介绍了使用SMS验证设备的电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过让设备发送短信到自身,并自动如果SMS已收到检查,以保证Android设备的电话号码。我怎样才能做到这一点?

I am trying to verify the phone number of an Android device by having the device send an SMS to itself, and automatically checking if the SMS has been received. How can I do this?

推荐答案

的对象和方法,你需要在 android.telephony 被发现,特别是 android.telephony.SmsManager android.telephony.SmsMessage

The objects and methods you will need are to be found in android.telephony, specifically android.telephony.SmsManager and android.telephony.SmsMessage.

首先,你需要获取权限来发送,并通过指定在AndroidManifest.xml中如下接收短信:

Firstly, you will need to acquire permissions to send and receive SMS by specifying the following in your AndroidManifest.xml:

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

然后,发送短信,你会使用 sendTextMessage sendMultipartTextMessage SmsManager的。既然你发送一个简单的验证消息,你只需要关心自己的第一个,目前。如果你发送的消息超过最大邮件大小,然后你将需要分割它,并使用多部分的方法。

Then, to send an SMS, you will use the sendTextMessage and sendMultipartTextMessage methods of SmsManager. Since you are sending a simple verification message, you need only concern yourself with the first, currently. If you were to send a message that exceeds the maximum message size, you would then need to split it up and use the multipart method.

sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)

再次给你相对简单的目标,你真的只需要提供 destinationAddress 文本参数,第一个是电话号码,第二个是该消息的内容。你可以通过的休息。例如:

Again, given your relatively simple objective, you really only need to supply the destinationAddress and the text parameters, the first being the phone number, the second being the message content. You can pass null to the rest. For example:

SmsManager sm = SmsManager.getDefault();
sm.sendTextMessage(number, null, msg, null, null);

要接收和阅读短信,你需要注册一个的BroadcastReceiver 与过滤器的 android.provider.Telephony.SMS_RECEIVED 的行动。你可以这样做,在你的清单,或在您的活动。如果不需要监视收到的每个短信,我会建议将其注册在你的活动只是,只要你需要它。

To receive and read an SMS, you will need to register a BroadcastReceiver with a filter for the android.provider.Telephony.SMS_RECEIVED action. You may do this in your manifest, or in your Activity. If you don't need to monitor every SMS received, I would recommend registering it in your Activity for just as long as you need it.

然后,在接收器的的onReceive 方法,可以使用以下方法来读取邮件内容,以验证:

Then, in the onReceive method of the receiver, you can use the following to read the message content to verify:

@Override
public void onReceive(Context context, Intent intent)
{
    Bundle extras = intent.getExtras();

    if (extras == null)
        return;

    Object[] pdus = (Object[]) extras.get("pdus");
    SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdus[0]);
    String origNumber = msg.getOriginatingAddress();
    String msgBody = msg.getMessageBody();
    ...
    ... 
}

如果你需要阅读的邮件的全部,你会遍历PDU的对象数组并连接返回的消息体,以获得完整的信息。但是,你只需要一个简短的消息,所以我们只需要检查的第一部分,的PDU [0] 。在上述code, origNumber 将会从中消息传来的电话号码。与此相比,给你发送的消息之一,并有亚去。

If you needed to read the entirety of a message, you would loop over the Object array of PDUs and concatenate the returned message bodies to get the complete message. But, you just require a short, simple message, so we need only check the first segment, pdus[0]. In the above code, origNumber will be the phone number from which the message came. Compare this to the one to which you sent the message, and there ya go.

这篇关于使用SMS验证设备的电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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