我正在尝试不使用第三方进行手机号码验证。为此我的逻辑是: -
这个逻辑是正确还是需要进行一些修改?
发送短信我正在使用此代码,但不知道我如何收到并验证号码。
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);
更新 -
未在三星设备上收到消息。
答案 0 :(得分:8)
首先,我在10000到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();
上述代码说明:
用户确认发送短信后,会将短信与输入的号码一起发送,并且该号码应该是用户自己使用的号码。因此,我们将等待带有代码的短信,接收短信并阅读其内容,我们将创建一个BroadCastReceiver
来收听传入的短信。
现在我们还需要启动一个计时器,以便我们只等待2分钟的短信,所以我们在2分钟结束时开始CountDownTimer
2分钟,倒数计时器将取消注册接收器。即使在传入的短信中接收到代码,接收器也会被注销,以便我们可以释放资源。
readSMS的代码,如果发现代码为true则返回true,否则返回false,
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;
}
使用以下方法发送短信:
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中,您需要拥有以下权限:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
答案 1 :(得分:0)
您可以使用内容提供商访问短信收件箱!
ContentResolver contentResolver =getContentResolver();
Uri uri = Uri.parse("content://sms/inbox");
//query here