Android短信验证码 Android短信验证码(用的Mob短信验证)
屠龙灬世家 人气:0想了解Android短信验证码(用的Mob短信验证)的相关内容吗,屠龙灬世家在本文为您仔细讲解Android短信验证码的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android短信验证码,Mob短信验证,Android短信验证,下面大家一起来学习吧。
Android短信验证码功能,供大家参考,具体内容如下
1、参考资料
Mob网站:http://www.mob.com/
Mob在Github上的例子:https://github.com/MobClub/SMSSDK-for-Android
教程:Mob短信验证的具体使用
教程的源代码: http://git.oschina.net/lizhanqi/MobSMSDemo
Dialog教程:Android Dialog对话框
定时器教程:Android 实现定时器的四种方式
2、前期准备
(1)Mob网站上下载SMS对应的SDK,解压出来为图1,打开图1,将其中的文件拷贝到as项目中的libs文件夹下,另外在add as library。
图1
图2
(2)在as项目中的AndroidManifest.xml文件中添加图3所需权限,由于SMS权限为危险权限,所以还要动态申请,这在下面代码中会有体现。注意权限是写在application外面,不是里面。
图3
(3)如果图4中的targetSdkVersion大于22,那么在代码中必须动态申请权限。
图4
(4)在as项目中的图7中的build.gradle文件中添加图5和图6红色框中内容。
图5
图6
图7
(5)在Mob网站上申请App Key和App Secret。
图8
(6)Toast
3、代码(没整理,将就看)
package com.gxu.lepao.activity; import android.Manifest; import android.app.Dialog; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.os.CountDownTimer; import android.os.Handler; import android.os.Message; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.gxu.lepao.R; import com.gxu.lepao.model.UserInfo; import org.litepal.crud.DataSupport;//LitePal开源数据库框架 import java.util.List; import cn.smssdk.EventHandler;//Mob短信验证码 import cn.smssdk.SMSSDK;//Mob短信验证码 /** * Created by ljy on 2017-05-21. * 获取和验证短信验证码界面 */ public class RegisterActivity extends BaseActivity { private EditText phoneEdit; private EditText smsEdit; private Button getSms; private Button submitSms; private TextView login; private TextView reset; private CountDownTimer countDownTimer; //倒计时 private int TIME = 60; //这是中国区号,如果需要其他国家列表,可以使用getSupportedCountries();获得国家区号 public String country = "86"; //APPKEY private static String appKey = "1e0d15118480d"; // 填写从短信SDK应用后台注册得到的APPSECRET private static String appSecret = "daa97d794e2436abcae624ac3785d2ef"; //表示是否使用了registerEventHandler private boolean ready; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d("RegisterActivity","Task; id is " + getTaskId()); setContentView(R.layout.activity_register); // 初始化界面 initView(); //初始化SMSSDK initSDK(); getSms.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { boolean isLogin = false; //判断手机号码是否已注册过 isLogin = isRegister(); if(!TextUtils.isEmpty(phoneEdit.getText().toString())){ if(!isLogin){ //发送短信权限 if(ContextCompat.checkSelfPermission(RegisterActivity.this ,Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED){ //显示申请权限弹窗 showRequestPermission(); }else{ //弹窗确认 alterWarning(); } }else{ Toast.makeText(RegisterActivity.this,"此手机号码已经注册过",Toast.LENGTH_SHORT).show(); } }else{ Toast.makeText(RegisterActivity.this,"请输入手机号码",Toast.LENGTH_SHORT).show(); } } }); submitSms.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { String sms = smsEdit.getText().toString(); String phone = phoneEdit.getText().toString(); if(!TextUtils.isEmpty(sms)){ SMSSDK.submitVerificationCode( country, phone, sms); }else{ Toast.makeText(RegisterActivity.this,"请输入验证码",Toast.LENGTH_SHORT).show(); } } }); login.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { Intent intent = new Intent(RegisterActivity.this,LoginActivity.class); startActivity(intent); } }); reset.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { Intent intent = new Intent(RegisterActivity.this,ResetPwdActivity.class); startActivity(intent); } }); } //申请权限弹窗 private void showRequestPermission(){ //先new出一个监听器,设置好监听 DialogInterface.OnClickListener dialogOnclicListener=new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { switch(which){ case Dialog.BUTTON_POSITIVE: //接收短信权限 ActivityCompat.requestPermissions(RegisterActivity.this,new String[]{Manifest.permission.RECEIVE_SMS},1); //发送短信权限 ActivityCompat.requestPermissions(RegisterActivity.this,new String[]{Manifest.permission.SEND_SMS},2); //弹窗确认 alterWarning(); break; case Dialog.BUTTON_NEGATIVE: Toast.makeText(RegisterActivity.this, "拒绝" + which, Toast.LENGTH_SHORT).show(); break; } } }; //dialog参数设置 AlertDialog.Builder builder=new AlertDialog.Builder(RegisterActivity.this); //先得到构造器 builder.setTitle("申请权限"); //设置标题 builder.setMessage("要允许lepao接收验证码短信吗?"); //设置内容 builder.setIcon(R.mipmap.ic_launcher);//设置图标,图片id即可 builder.setPositiveButton("允许",dialogOnclicListener); builder.setNegativeButton("拒绝", dialogOnclicListener); builder.create().show(); } //申请权限 public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode){ case 1: if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ //initSDK(); Toast.makeText(this,"你授权。。。接收短信权限",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this,"你拒绝。。。接收短信权限",Toast.LENGTH_SHORT).show(); } break; case 2: if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ Toast.makeText(this,"你授权。。。发送短信权限",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this,"你拒绝。。。发送短信权限",Toast.LENGTH_SHORT).show(); } break; default: } } // 初始化短信SDK private void initSDK() { SMSSDK.initSDK(this, appKey, appSecret, true); //final Handler handler = new Handler(); EventHandler eventHandler = new EventHandler() { public void afterEvent(int event, int result, Object data) { //回调完成 if (result == SMSSDK.RESULT_COMPLETE) { //验证码验证成功 if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) { toast("验证成功"); Intent intent = new Intent(RegisterActivity.this,SetPwdActivity.class); intent.putExtra("phone",phoneEdit.getText().toString()); startActivity(intent); finish(); } //已发送验证码 else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) { toast("验证码已经发送"); } else { ((Throwable) data).printStackTrace(); String str = data.toString(); toast(str); } } if(result==SMSSDK.RESULT_ERROR) { toast("验证码错误"); } } }; // 注册回调监听接口 SMSSDK.registerEventHandler(eventHandler); ready = true; } //吐司的一个小方法 private void toast(final String str) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(RegisterActivity.this, str, Toast.LENGTH_SHORT).show(); } }); } //销毁短信注册 @Override protected void onDestroy() { // 注销回调接口registerEventHandler必须和unregisterEventHandler配套使用,否则可能造成内存泄漏。 if(ready){ SMSSDK.unregisterAllEventHandler(); } super.onDestroy(); } //初始化界面 private void initView(){ phoneEdit = (EditText) findViewById(R.id.phone); smsEdit = (EditText) findViewById(R.id.sms); getSms = (Button) findViewById(R.id.getSms); submitSms = (Button) findViewById(R.id.submitSms); login = (TextView) findViewById(R.id.login); reset = (TextView) findViewById(R.id.reset); } //弹窗确认 private void alterWarning(){ //先new出一个监听器,设置好监听 DialogInterface.OnClickListener dialogOnclicListener=new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { switch(which){ case Dialog.BUTTON_POSITIVE: dialog.dismiss(); //通过sdk发送短信验证(请求获取短信验证码,在监听(eventHandle)中返回) SMSSDK.getVerificationCode(country, phoneEdit.getText().toString()); //设置获取验证码按钮不能点击 getSms.setClickable(false); //倒计时,执行次数为(TIME+1)*1000/1000,countDownTimer每次执行间隔:1000(单位为毫秒) countDownTimer = new CountDownTimer((TIME+1)*1000, 1000) { @Override public void onTick(long millisUntilFinished) { getSms.setText(TIME-- + "秒后再次获取验证码"); } @Override public void onFinish() { //设置获取验证码按钮可以点击 getSms.setClickable(true); getSms.setText("点击获取短信验证码"); } }; countDownTimer.start(); break; case Dialog.BUTTON_NEGATIVE: dialog.dismiss(); Toast.makeText(RegisterActivity.this, "已取消", Toast.LENGTH_SHORT).show(); break; } } }; //dialog参数设置 AlertDialog.Builder builder=new AlertDialog.Builder(this); //先得到构造器 builder.setTitle("发送短信"); //设置标题 builder.setMessage("我们将把验证码发送到以下号码:\n"+country+"-"+phoneEdit.getText().toString()); //设置内容 builder.setIcon(R.drawable.icon);//设置图标,图片id即可 builder.setPositiveButton("确认",dialogOnclicListener); builder.setNegativeButton("取消", dialogOnclicListener); builder.create().show(); } //判断手机号码是否已注册过 public boolean isRegister(){ boolean isLogin = false; 。。。。。299 return isLogin; } }
加载全部内容