Android应用开发之手机验证码
白羽 2018-12-19 来源 :网络 阅读 379 评论 0

摘要:本文将带你了解Android应用开发android开发之手机验证码,希望本文对大家学Android有所帮助。

    本文将带你了解Android应用开发android开发之手机验证码,希望本文对大家学Android有所帮助。



        

android开发之手机验证码。

在build.gradle中:


   

apply plugin: 'com.mob.sdk'

MobSDK {

 appKey "25ee56c67f230"

 appSecret "be0d569934eeb6d8a22142a7f5d9e0fe"

 SMSSDK {}

}

   

添加依赖:


   

compile <strong>'com.github.yidun:captcha-android-demo:2.4.2'</strong>

   


   

<strong>===============================XML=============================</strong>

   



   

<!--xml version="1.0" encoding="utf-8"-->

<relativelayout android:layout_height="match_parent" android:layout_width="match_parent" tools:context=".MainActivity" xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" xmlns:tools="https://schemas.android.com/tools">

 

 <edittext android:hint="请输入手机号" android:id="@+id/editText" android:layout_alignparentstart="true" android:layout_height="wrap_content" android:layout_margintop="210dp" android:layout_tostartof="@+id/button" android:layout_width="wrap_content"><button android:id="@+id/button" android:layout_alignparentend="true" android:layout_alignparenttop="true" android:layout_height="wrap_content" android:layout_margintop="210dp" android:layout_width="wrap_content" android:text="获取验证码"></button><button android:id="@+id/button2" android:layout_alignstart="@+id/button" android:layout_below="@+id/button" android:layout_height="wrap_content" android:layout_margintop="20dp" android:layout_width="wrap_content" android:text="点击验证">

 

 <edittext android:id="@+id/editText2" android:layout_alignend="@+id/editText" android:layout_alignparentstart="true" android:layout_below="@+id/editText" android:layout_height="wrap_content" android:layout_margintop="30dp" android:layout_width="wrap_content">

 

</edittext></button></edittext></relativelayout>

   


=========================MainActivity=======================================


   

package com.example.phone;

 

import android.annotation.SuppressLint;

import android.content.Intent;

import android.os.Handler;

import android.os.Message;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

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 org.json.JSONObject;

 

import cn.smssdk.EventHandler;

import cn.smssdk.SMSSDK;

import cn.smssdk.utils.SMSLog;

 

public class MainActivity extends AppCompatActivity {

 private Button btn1, btn2;

 private EditText edt1, edt2;

 private String phone;

 @Override

 protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  btn1 = (Button) findViewById(R.id.button);

  btn2 = (Button) findViewById(R.id.button2);

  edt1 = (EditText) findViewById(R.id.editText);

  edt2 = (EditText) findViewById(R.id.editText2);

  EventHandler eh = 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;

 mHandler.sendMessage(msg);

}

 

  };

  SMSSDK.registerEventHandler(eh);

  btn1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// Toast.makeText(getApplicationContext(), "123", Toast.LENGTH_SHORT).show();

 //SMSSDK.getVerificationCode("1", "4432487596");

 SMSSDK.getVerificationCode("86", edt1.getText().toString());

 phone = edt1.getText().toString();

}

  });

 

  btn2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

 SMSSDK.submitVerificationCode("86", phone, edt2.getText().toString());

}

  });

 }

 @SuppressLint("HandlerLeak")

 Handler mHandler = new Handler() {

  public void handleMessage(Message msg) {

 

// TODO Auto-generated method stub

super.handleMessage(msg);

int event = msg.arg1;

int result = msg.arg2;

Object data = msg.obj;

Log.e("event", "event=" + event);

if (result == SMSSDK.RESULT_COMPLETE) {

 System.out.println("--------result" + event);

 //短信注册成功后,返回MainActivity,然后提示新好友

 if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {//提交验证码成功

  String s = edt1.getText().toString();

  Toast.makeText(getApplicationContext(), "提交验证码成功", Toast.LENGTH_SHORT).show();

  Intent intent = new Intent(MainActivity.this, AgainActivity.class);

  intent.putExtra("moblie",s);

  startActivity(intent);

  finish();

 

 } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {

  //已经验证

  Toast.makeText(getApplicationContext(), "验证码已经发送", Toast.LENGTH_SHORT).show();

 } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {

  //已经验证

  Toast.makeText(getApplicationContext(), "获取国家列表成功", Toast.LENGTH_SHORT).show();

//  textV.setText(data.toString());

 }

 

} else {

//  ((Throwable) data).printStackTrace();

//  Toast.makeText(MainActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();

//Toast.makeText(MainActivity.this, "123", Toast.LENGTH_SHORT).show();

 int status = 0;

 try {

  ((Throwable) data).printStackTrace();

  Throwable throwable = (Throwable) data;

 

  JSONObject object = new JSONObject(throwable.getMessage());

  String des = object.optString("detail");

  status = object.optInt("status");

  if (!TextUtils.isEmpty(des)) {

Toast.makeText(MainActivity.this, des, Toast.LENGTH_SHORT).show();

return;

  }

 } catch (Exception e) {

  SMSLog.getInstance().w(e);

 }

}

  }

 };

}

   

          

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!



本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程