转载

android短信验证

android短信验证:

第一步下载SMSSDK

首先注册必须注册,获取相应的 APPKEY, APPSECRTE 之后的程序会用到

下载地址

将MobCommons-2017.0321.1624.jar MobTools2-017.0321.1624.jar, 和SMSDk-2.1.4.aar

放入app的lib文件夹

之后进行配置:

在app的build.gradle加入以下文本

repositories {
    flatDir {
        dirs "libs"  }
}
在添加依赖项加入
compile name:'SMSSDK-2.1.4',ext:'aar'

2:代码部分

UI代码

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:background="#FAFAFA" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:orientation="vertical" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#64B5F6" android:minHeight="?attr/actionBarSize" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="手机号注册" android:textColor="@android:color/white" android:textSize="20sp" android:textStyle="bold" /> </android.support.v7.widget.Toolbar> <com.rengwuxian.materialedittext.MaterialEditText android:id="@+id/phone_num_text" android:layout_width="match_parent" android:layout_height="70dp" android:hint=" 手机号码" app:met_primaryColor="@android:color/white" /> <com.rengwuxian.materialedittext.MaterialEditText android:layout_width="match_parent" android:layout_height="70dp" android:hint=" 6-12位字母数字组合密码" app:met_primaryColor="@android:color/white" /> <com.rengwuxian.materialedittext.MaterialEditText android:layout_width="match_parent" android:layout_height="70dp" android:hint=" 确认密码" app:met_primaryColor="@android:color/white" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.rengwuxian.materialedittext.MaterialEditText android:id="@+id/msg_verify_text" android:layout_width="0dp" android:layout_weight="3" android:layout_height="70dp" android:hint=" 短信验证码" app:met_primaryColor="@android:color/white" /> <Button android:id="@+id/get_msg_verify_button" android:background="@drawable/rect" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="获取验证码" /> </LinearLayout> <Button android:id="@+id/register_button" android:background="@drawable/rect" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="50dp" android:text="注册"/> </LinearLayout> 



逻辑代码:

package com.example.administrator.logintest; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.rengwuxian.materialedittext.MaterialEditText; import cn.smssdk.EventHandler; import cn.smssdk.SMSSDK; import static android.R.id.message; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private static final String APP_KEY = "获得的APPKEY"; private static final String APP_SECRET = "获得的APP_SECRET"; private String phoneNumber; private MaterialEditText phoneText; private MaterialEditText msgVerifyText; private Button getMsgVerifyButton; private Button registerButton; private Handler handle = new Handler() { @Override public void handleMessage(Message message) { Log.d("MainActivity", "handleMessage"); int event = message.arg1; int result = message.arg2; Object data = message.obj; if(result == SMSSDK.RESULT_COMPLETE) { if(event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) { Toast.makeText(MainActivity.this, "验证成功", Toast.LENGTH_SHORT).show(); Log.d("MainActivity", "验证成功"); }else if(event == SMSSDK.EVENT_GET_VERIFICATION_CODE) { Toast.makeText(MainActivity.this, "获取验证码成功", Toast.LENGTH_SHORT).show(); Log.d("MainActivity", "获取验证码成功"); }else if(event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) { //返回支持发送验证码的国家列表 } } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if(getSupportActionBar() != null) { getSupportActionBar().hide(); } phoneText = (MaterialEditText) findViewById(R.id.phone_num_text); msgVerifyText = (MaterialEditText) findViewById(R.id.msg_verify_text); getMsgVerifyButton = (Button) findViewById(R.id.get_msg_verify_button); registerButton = (Button) findViewById(R.id.register_button); registerButton.setOnClickListener(this); getMsgVerifyButton.setOnClickListener(this); initSMSSDK(); } public void initSMSSDK() { SMSSDK.initSDK(this, APP_KEY, APP_SECRET); 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; handle.sendMessage(msg); } }; SMSSDK.registerEventHandler(eventHandler); } protected void onDestroy() { // 销毁回调监听接口 SMSSDK.unregisterAllEventHandler(); super.onDestroy(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.get_msg_verify_button: phoneNumber = phoneText.getText().toString().trim(); Log.d("MainActivity", phoneNumber); if (!TextUtils.isEmpty(phoneNumber)) { SMSSDK.getVerificationCode("86", phoneNumber);//获取短信 Toast.makeText(this,"已经提交",Toast.LENGTH_LONG).show(); }else { Toast.makeText(this, "电话号码不能为空", Toast.LENGTH_LONG).show(); } break; case R.id.register_button: String number = msgVerifyText.getText().toString().trim(); Log.d("MainActivity", number); if (!TextUtils.isEmpty(number)) { SMSSDK.submitVerificationCode("86", phoneNumber,number);//验证短信 }else { Toast.makeText(this, "验证码不能为空", Toast.LENGTH_LONG).show(); return; } break; default: break; } } }  



正文到此结束
本文目录