• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
戈瑾
博客园    首页    新随笔    联系   管理    订阅  订阅
android验证码登录

今天完成了校园二手交易平台的验证码功能,我用的是Mob平台+SMSSDK。

1、注册mob平台账号

2、在mob平台配置SMSSDK环境

在mob平台创建应用

获取自己的App Key和App Secret

下载需要的SDK和SMSSDK

3、把SMSSDK的代码添加到android

在AndroidManifest中加入权限

在Project和Moudule中的builder.gradle中加入代码

核心代码:

LoginYzmActivity.java文件

 

 1 package com.example.secondhand;
 2 
 3 import android.content.Intent;
 4 import android.os.Bundle;
 5 import android.text.TextUtils;
 6 import android.view.View;
 7 import android.widget.EditText;
 8 import android.widget.Toast;
 9 
10 import androidx.appcompat.app.AppCompatActivity;
11 
12 import com.mob.MobSDK;
13 
14 import org.json.JSONException;
15 import org.json.JSONObject;
16 
17 import cn.smssdk.EventHandler;
18 import cn.smssdk.SMSSDK;
19 
20 import static com.example.secondhand.MyApplication.user;
21 
22 public class LoginYzmActivity extends AppCompatActivity {
23     EventHandler handler;
24     EditText editText;
25     EditText editText1;
26     @Override
27     protected void onCreate(Bundle savedInstanceState) {
28         super.onCreate(savedInstanceState);
29         setContentView(R.layout.activity_login_yzm);
30         MobSDK.init(this, "331788cf59610","2154bdb99e9bac529f660094d3f41f08");       //记得此处修改为你的AppKey和AppSecret
31         editText=findViewById(R.id.etYzm);
32         editText1=findViewById(R.id.etPhone);
33         handler = new EventHandler(){
34             @Override
35             public void afterEvent(int event, int result, Object data) {
36                 if (result == SMSSDK.RESULT_COMPLETE){
37                     //回调完成
38                     if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
39                         //提交验证码成功
40                         runOnUiThread(new Runnable() {
41                             @Override
42                             public void run() {
43                                 Toast.makeText(LoginYzmActivity.this,"验证成功", Toast.LENGTH_SHORT).show();
44                                 Intent intent = new Intent(LoginYzmActivity.this,HomeActivity.class);
45                                 startActivity(intent);
46                             }
47                         });
48 
49                     }else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
50                         //获取验证码成功
51                         runOnUiThread(new Runnable() {
52                             @Override
53                             public void run() {
54                                 Toast.makeText(LoginYzmActivity.this,"验证码已发送", Toast.LENGTH_SHORT).show();
55                     }
56                 });
57                     }else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){
58                     }
59                 }else{
60                     ((Throwable)data).printStackTrace();
61                     Throwable throwable = (Throwable) data;
62                     try {
63                         JSONObject obj = new JSONObject(throwable.getMessage());
64                         final String des = obj.optString("detail");
65                         if (!TextUtils.isEmpty(des)){
66                             runOnUiThread(new Runnable() {
67                                 @Override
68                                 public void run() {
69                                     Toast.makeText(LoginYzmActivity.this,"提交错误信息", Toast.LENGTH_SHORT).show();
70                                 }
71                             });
72                         }
73                     } catch (JSONException e) {
74                         e.printStackTrace();
75                     }
76 
77                 }
78             }
79         };
80 
81         SMSSDK.registerEventHandler(handler);
82     }
83 
84     //提交验证
85     public void tijiao(View view) {
86         String number = editText.getText().toString();
87         String phone=editText1.getText().toString();
88         SMSSDK.submitVerificationCode("86",phone,number);
89     }
90 
91     //点击发送验证码
92     public void play(View view) {
93         //获取验证码
94         String phone=editText1.getText().toString();
95         SMSSDK.getVerificationCode("86",phone);
96     }
97 }

activity_login_yzm.xml文件:

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     xmlns:app="http://schemas.android.com/apk/res-auto"
  4     xmlns:tools="http://schemas.android.com/tools"
  5     android:layout_width="match_parent"
  6     android:layout_height="match_parent"
  7     android:orientation="vertical"
  8     tools:context=".LoginYzmActivity">
  9 
 10     <RelativeLayout
 11         android:layout_width="match_parent"
 12         android:layout_height="48dp"
 13         android:background="@color/colorHead"
 14 
 15         android:orientation="horizontal">
 16 
 17         <ImageView
 18             android:id="@+id/backArrow"
 19             android:layout_width="wrap_content"
 20             android:layout_height="wrap_content"
 21             android:layout_centerVertical="true"
 22             android:padding="10dp"
 23             android:src="@mipmap/back_arrow" />
 24 
 25         <TextView
 26             android:layout_width="wrap_content"
 27             android:layout_height="wrap_content"
 28             android:layout_centerInParent="true"
 29             android:text="登录"
 30             android:id="@+id/tvTitle"
 31             android:textColor="@color/white"
 32             android:textSize="18sp"
 33             android:textStyle="bold" />
 34 
 35     </RelativeLayout>
 36 
 37     <RelativeLayout
 38         android:layout_width="match_parent"
 39         android:layout_height="wrap_content"
 40         android:layout_marginTop="48dp"
 41         android:layout_weight="1">
 42 
 43         <ImageView
 44             android:layout_width="108dp"
 45             android:layout_height="108dp"
 46             android:layout_centerInParent="true"
 47             android:scaleType="centerCrop"
 48             android:src="@mipmap/ic_launcher" />
 49     </RelativeLayout>
 50 
 51     <RelativeLayout
 52         android:layout_width="match_parent"
 53         android:layout_height="40dp"
 54         android:layout_marginTop="48dp">
 55 
 56         <ImageView
 57             android:layout_width="32dp"
 58             android:layout_height="32dp"
 59             android:layout_marginLeft="26dp"
 60             android:layout_marginBottom="4dp"
 61             android:layout_alignParentBottom="true"
 62             android:src="@drawable/ic_person_outline_black_24dp"/>
 63 
 64         <EditText
 65             android:id="@+id/etPhone"
 66             android:layout_width="match_parent"
 67             android:layout_height="wrap_content"
 68             android:layout_alignParentBottom="true"
 69             android:layout_gravity="center"
 70             android:layout_marginStart="56dp"
 71             android:layout_marginEnd="45dp"
 72             android:layout_marginBottom="0dp"
 73             android:hint="请输入您的手机号码"
 74             android:inputType="text"
 75             android:maxLines="1" />
 76         <Button
 77             android:id="@+id/get_yzm"
 78             android:layout_width="80dp"
 79             android:layout_height="wrap_content"
 80             android:background="@color/colorHead"
 81             android:text="验证码"
 82             android:onClick="play"
 83             android:textColor="@color/black"
 84             android:textSize="14sp"
 85             android:layout_alignParentEnd="true"
 86             android:layout_alignParentRight="true"
 87             android:layout_alignParentBottom="true"
 88             android:layout_marginEnd="42dp"
 89             android:layout_marginRight="42dp"
 90             android:layout_marginBottom="10dp"
 91             />
 92     </RelativeLayout>
 93 
 94     <RelativeLayout
 95         android:layout_width="match_parent"
 96         android:layout_height="40dp"
 97         android:layout_marginTop="16dp">
 98 
 99         <ImageView
100             android:layout_width="32dp"
101             android:layout_height="32dp"
102             android:layout_marginBottom="4dp"
103             android:layout_marginLeft="26dp"
104             android:layout_alignParentBottom="true"
105             android:src="@drawable/ic_lock_outline_black_24dp"/>
106 
107         <EditText
108             android:id="@+id/etYzm"
109             android:layout_width="match_parent"
110             android:layout_height="wrap_content"
111             android:layout_alignParentBottom="true"
112             android:layout_marginStart="56dp"
113             android:layout_marginEnd="45dp"
114             android:layout_marginBottom="0dp"
115             android:hint="请输入手机验证码"
116             android:maxLines="1" />
117 
118     </RelativeLayout>
119 
120 
121 
122     <Button
123         android:id="@+id/btnLogin"
124         android:layout_width="330dp"
125         android:layout_height="wrap_content"
126         android:layout_gravity="center_horizontal"
127         android:layout_marginTop="60dp"
128         android:layout_marginBottom="140dp"
129         android:background="@color/colorHead"
130         android:text="登录"
131         android:onClick="tijiao"
132         android:textColor="@color/black"
133         android:textSize="14sp"
134         />
135 
136 </LinearLayout>

 

 

 

 

posted on 2021-04-26 22:14  戈瑾  阅读(283)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3