使用 MobPus 免费短信验证玛
官方文档:MobTech集成文档-MobTech
https://www.mob.com/

MOB网页申请部分:

  1. 进入开发者平台
  2. 创建应用
  3. 填写信息

    包名:应用程序中获取

MD5 签名获取:

1.方法一


选择 APK-> 选择 Create new-> 打开文件路径



新建时需要全部填写,密码不可以忘记

进入文件夹 , 修改后缀名并解压



进入 META-INF 后在终端中打开,输入以下命令(ubantu 看不到 md5)
keytool -printcert -file CERT.RSA

或以下方法,但须输入密码。(ubantu 看不到 md5)
keytool -list -v -keystore loginkey.jks

2.方法二(个人感觉比方法一好用)

百度搜索“微信开放平台”,或直接进入网址:
https://open.weixin.qq.com/
进入资源中心 -> 资源下载 ->Android 资源下载



下载签名生成工具
https://res.wx.qq.com/wxdoc/dist/assets/media/Gen_Signature_Android.e481f889.zip
下载到手机后安装,输入 APP 包名即可获取签名(md5)

代码参考:

1. build.gradle中添加
plugins {id 'com.android.application'}apply plugin: 'com.mob.sdk'MobSDK {appKey "38ec31857c79c"appSecret "b346b0acba1d255b823ed1b443c29d7a"SMSSDK {SMSSDK}}
2. MainActivity
package com.example.login_api;import androidx.appcompat.app.AppCompatActivity;import android.annotation.SuppressLint;import android.content.pm.PackageInfo;import android.content.pm.PackageManager;import android.content.pm.Signature;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import com.mob.MobSDK;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.math.BigInteger;import java.nio.MappedByteBuffer;import java.nio.channels.FileChannel;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.Timer;import java.util.TimerTask;import cn.smssdk.EventHandler;import cn.smssdk.SMSSDK;public class MainActivity extends AppCompatActivity implements View.OnClickListener {EventHandler eventHandler;EditText editTextPhone,editTextNumber;Button get_code_id,login_id;Timer timer;int count = 60;Handler handler = new Handler(){public void handleMessage(Message msg) {int tag = msg.what;switch (tag){case 1:int arg = msg.arg1;if(arg==1){get_code_id.setText("重新获取");//计时结束停止计时把值恢复count = 60;timer.cancel();get_code_id.setEnabled(true);}else{get_code_id.setText(count+"");}break;case 2:Toast.makeText(MainActivity.this,"获取短信验证码成功",Toast.LENGTH_LONG).show();break;case 3:Log.i("Codr","获取短信验证码失败");Toast.makeText(MainActivity.this,"获取短信验证码失败",Toast.LENGTH_LONG).show();break;default:break;}};};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);MobSDK.submitPolicyGrantResult(true);init();}private void init(){editTextPhone = findViewById(R.id.editTextPhone);editTextNumber = findViewById(R.id.editTextNumber);get_code_id = findViewById(R.id.get_code_id);get_code_id.setOnClickListener(this::onClick);login_id = findViewById(R.id.login_id);login_id.setOnClickListener(this::onClick);eventHandler = new EventHandler() {@Overridepublic void afterEvent(int event, int result, Object data) {// TODO 此处为子线程!不可直接处理UI线程!处理后续操作需传到主线程中操作!if (result == SMSSDK.RESULT_COMPLETE) {//成功回调if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {//提交短信、语音验证码成功} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {Message message = new Message();message.what = 2;handler.sendMessage(message);} else if (event == SMSSDK.EVENT_GET_VOICE_VERIFICATION_CODE) {//获取语音验证码成功Message message = new Message();message.what = 2;handler.sendMessage(message);}} else if (result == SMSSDK.RESULT_ERROR) {//失败回调Message message = new Message();message.what = 3;handler.sendMessage(message);} else {//其他失败回调((Throwable) data).printStackTrace();}}};SMSSDK.registerEventHandler(eventHandler); //注册短信回调}@Overridepublic void onClick(View view) {String phone = "";String code = "";int id = view.getId();if (id == R.id.get_code_id) {phone = editTextPhone.getText().toString().trim();if (TextUtils.isEmpty(phone)) {Toast.makeText(this, "请输入手机号码", Toast.LENGTH_LONG).show();} else {//倒计时CountdownStart();getVerificationCode("86", phone);}} else if (id == R.id.login_id) {phone = editTextPhone.getText().toString().trim();code = editTextNumber.getText().toString().trim();if (TextUtils.isEmpty(phone)) {Toast.makeText(this, "请输入手机号码", Toast.LENGTH_LONG).show();} else if (TextUtils.isEmpty(code)) {Toast.makeText(this, "请输入验证码", Toast.LENGTH_LONG).show();} else {//登录逻辑}}}/** * cn.smssdk.SMSSDK.class * 请求文本验证码 * @param country 国家区号 * @param phone 手机号 */public static void getVerificationCode(String country, String phone){//获取短信验证码SMSSDK.getVerificationCode(country,phone);}/**cn.smssdk.SMSSDK.class * 请求文本验证码(带模板编号) * @param tempCode模板编号 * @param country 国家区号 * @param phone 手机号 */public static void getVerificationCode(String tempCode,String country, String phone){//获取短信验证码SMSSDK.getVerificationCode(tempCode,country,phone);}//倒计时函数private void CountdownStart(){get_code_id.setEnabled(false);timer = new Timer();timer.schedule(new TimerTask() {@Overridepublic void run() {Message message = new Message();message.what = 1;message.arg1 = count;if(count!=0){count--;}else {return;}handler.sendMessage(message);}}, 1000,1000);}@Overrideprotected void onDestroy() {super.onDestroy();// 使用完EventHandler需注销,否则可能出现内存泄漏SMSSDK.unregisterEventHandler(eventHandler);}}