遇到困难:并没有找到在客户端直接发送验证码到邮箱的代码
解决困难:之前没有找到免费发送手机验证码的接口,所以打算用邮箱发送验证码,后来有个同学告诉我用mob可以免费发送短信,于是改成发送短信验证码。
步骤:
1. 第一步当然是注册账号
2. 点击 进入后台→选择 SecurityCodeSDK→点击 立即使用→选择应用的平台→获取App key和App Secret
3. 下载sdk,解压后是这样的:
4. 之后安装官网导入sdk[官网链接]http://wiki.mob.com/android-%E7%9F%AD%E4%BF%A1sdk%E9%9B%86%E6%88%90%E6%96%87%E6%A1%A3/
5. 配置好后,添加权限
6. 我这里是自定义界面,当然也可以使用系统自带界面。代码调用:
//初始化SMSSDK private void initSDK() { SMSSDK.initSDK(forgetPassword.this, "18e3768f516a3", "e05f202526cd41dcf17d2498d6a7aeeb"); EventHandler eventHandler = new EventHandler() { @Override public void afterEvent(int event, int result, Object data) { Message msg = new Message(); msg.arg1 = event; msg.arg2 = result; msg.obj = data; msg.what = SMSDDK_HANDLER; handler.sendMessage(msg); } }; // 注册回调监听接口 SMSSDK.registerEventHandler(eventHandler); }
1 getNumber.setOnClickListener(new View.OnClickListener() { 2 @Override 3 public void onClick(View v) { 4 userPhone = phoneNumber.getText().toString(); 5 new AlertDialog.Builder(forgetPassword.this) 6 .setTitle("发送短信") 7 .setMessage("我们将把验证码发送到以下号码:\n" + "+86:" + userPhone) 8 .setPositiveButton("确定", new DialogInterface.OnClickListener() { 9 @Override 10 public void onClick(DialogInterface dialog, int which) { 11 SMSSDK.getVerificationCode("86", userPhone); 12 getNumber.setClickable(false); 13 new Thread(new Runnable() { 14 @Override 15 public void run() { 16 for (int i = 60; i > 0; i--) { 17 handler.sendEmptyMessage(CODE_ING); 18 if (i <= 0) { 19 break; 20 } 21 try { 22 Thread.sleep(1000); 23 } catch (InterruptedException e) { 24 e.printStackTrace(); 25 } 26 } 27 handler.sendEmptyMessage(CODE_REPEAT); 28 } 29 }).start(); 30 } 31 }) 32 .create() 33 .show(); 34 } 35 });
check.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SMSSDK.submitVerificationCode("86", userPhone, checkNunber.getText().toString());//对验证码进行验证->回调函数 } });
Handler handler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case CODE_ING://已发送,倒计时 getNumber.setText("重新发送(" + --TIME + "s)"); break; case CODE_REPEAT://重新发送 getNumber.setText("获取验证码"); getNumber.setClickable(true); break; case SMSDDK_HANDLER: int event = msg.arg1; int result = msg.arg2; Object data = msg.obj; //回调完成 if (result == SMSSDK.RESULT_COMPLETE) { //验证码验证成功 if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) { Toast.makeText(forgetPassword.this, "验证成功", Toast.LENGTH_LONG).show(); } //已发送验证码 else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) { Toast.makeText(getApplicationContext(), "验证码已经发送", Toast.LENGTH_SHORT).show(); } else { ((Throwable) data).printStackTrace(); } } if(result==SMSSDK.RESULT_ERROR) { try { Throwable throwable = (Throwable) data; throwable.printStackTrace(); JSONObject object = new JSONObject(throwable.getMessage()); String des = object.optString("detail");//错误描述 int status = object.optInt("status");//错误代码 if (status > 0 && !TextUtils.isEmpty(des)) { Toast.makeText(getApplicationContext(), des, Toast.LENGTH_SHORT).show(); return; } } catch (Exception e) { //do something } } break; } } };