android studio 实现短信验证 登录

登录 http://www.mob.com/ 注册 创建项目

在这里插入图片描述

加入依赖

在这里插入图片描述
在这里插入图片描述

贴代码

classpath “com.mob.sdk:MobSDK:2018.0319.1724”

apply plugin: ‘com.mob.sdk’
// 在MobSDK的扩展中注册SMSSDK的相关信息
MobSDK {
appKey “2e2974aef81c0”
appSecret “1d35b871b02a9c62c62b5ff811bb5c47”
SMSSDK {}
}

不要加错了

权限

  <!-- 权限 短信验证-->
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.GET_TASKS"/>
    
 <!-- 短信验证 提供的activate -->
        <activity android:name="com.mob.tools.MobUIShell"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:windowSoftInputMode="stateAlwaysHidden|adjustResize"/>

# 接着帖 我的完整代码 MainActivity 


package wlt.com.zen;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;

public class MainActivity extends AppCompatActivity {

    private Button bdsjh,tijiao;

    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView=(TextView)this.findViewById(R.id.tvsms);
        //事件
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendCode(MainActivity.this);
            }
        });
        bdsjh=(Button)this.findViewById(R.id.bdsjh);
        tijiao=(Button)this.findViewById(R.id.tijiao);
        tijiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        bdsjh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(MainActivity.this,"功能暂时没有开通....",Toast.LENGTH_LONG).show();

            }
        });
    }
    public void sendCode(Context context) {
        RegisterPage page = new RegisterPage();
        //如果使用我们的ui,没有申请模板编号的情况下需传null
        page.setTempCode(null);
        page.setRegisterCallback(new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE) {

                    //回调完成
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                        //提交验证码成功  添加动作 
                        Intent intent=new Intent(MainActivity.this,ok_long_Activate.class);
                        startActivity(intent);
                    }else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
                        //获取验证码成功
                    }else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){
                        //返回支持发送验证码的国家列表
                    }

                } else{
                    // TODO 处理错误的结果
                }
            }
        });
        page.show(context);
    }

}



布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#FF7788"
    android:gravity="center"
    >

    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <TextView
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="用户名:"/>
    <EditText
        android:hint="请输入用户名"
        android:layout_width="181dp"
        android:layout_height="wrap_content"
        android:inputType="text" />
</LinearLayout>
    <LinearLayout
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:textSize="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密  码 :"/>
        <EditText
            android:hint="请输入密码"
            android:layout_width="181dp"
            android:layout_height="wrap_content"
            android:inputType="numberPassword"/>
    </LinearLayout>
    <LinearLayout
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
    <Button
        android:id="@+id/bdsjh"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登  录" />

    <Button
        android:id="@+id/tijiao"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="注  册" />
    </LinearLayout>

    <TextView
        android:id="@+id/tvsms"
        android:text="手机验证码登录"
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp">

    </TextView>

</LinearLayout>

验证成功跳转的activate 

package wlt.com.zen;

import android.app.Activity;
import android.os.Bundle;

public class ok_long_Activate extends Activity {

    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.oklong);
    }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent">

    <TextView
        android:textSize="30dp"
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="欢迎光临" />
</LinearLayout>

以下是文档 提供学习参考http://www.mob.com/ 路径

贴上 相关方法

/ 请求验证码,其中country表示国家代码,如“86”;phone表示手机号码,如“13800138000”
SMSSDK.getVerificationCode(country, phone);

/ 提交验证码,其中的code表示验证码,如“1357”

SMSSDK.submitVerificationCode(country, phone, code);
 

public void sendCode(Context context) {
    RegisterPage page = new RegisterPage();
    //如果使用我们的ui,没有申请模板编号的情况下需传null
    page.setTempCode(null);
    page.setRegisterCallback(new EventHandler() {
        public void afterEvent(int event, int result, Object data) {
            if (result == SMSSDK.RESULT_COMPLETE) {
                // 处理成功的结果
                HashMap<String,Object> phoneMap = (HashMap<String, Object>) data;
                // 国家代码,如“86”
                String country = (String) phoneMap.get("country"); 
                // 手机号码,如“13800138000”
                String phone = (String) phoneMap.get("phone"); 


				 这里有个问题  在提交验证码 code 参数 不知道 怎么弄出来  如果相遇大神 求解 一下

                // TODO 利用国家代码和手机号码进行后续的操作
            } else{
                // TODO 处理错误的结果
            }
        }
    });
    page.show(context);
}

// 使用完EventHandler需注销,否则可能出现内存泄漏

protected void onDestroy() {
    super.onDestroy();
    SMSSDK.unregisterEventHandler(eventHandler);
}

以上是是在参考文档下实现的 在真机上测试成功 复制代码可用 还有好多细节 不解 还需加油 。。
发布了5 篇原创文章 · 获赞 0 · 访问量 169

猜你喜欢

转载自blog.csdn.net/qq_38218307/article/details/104522504