Android实现页面短信验证功能
程序员文章站
2022-06-30 11:31:12
本文实例为大家分享了android实现页面短信验证功能的具体代码,供大家参考,具体内容如下
目标效果:
上一篇博文介绍的是使用sdk中自带的验证页面,需要进行多次跳...
本文实例为大家分享了android实现页面短信验证功能的具体代码,供大家参考,具体内容如下
目标效果:
上一篇博文介绍的是使用sdk中自带的验证页面,需要进行多次跳转,并且项目需要添加两个框架,多有不便,这个例子简化了短信验证的代码,但是不足的是功能不如原本那个完善。
1.新建项目,找到上一次下载的sdk文件中包含的smssdk项目,复制lib目录下的三个jar包,res目录下的raw文件夹和values目录下的两个xml文件到本项目。
2.activity_main.xml页面设置布局。
activity_main.xml页面:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" > <edittext android:id="@+id/etphone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:hint="请输入手机号" android:layout_margintop="45dp" android:ems="10" > <requestfocus /> </edittext> <edittext android:id="@+id/etnumber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/etphone" android:hint="请输入验证码" android:layout_margintop="26dp" android:ems="10" /> <button android:id="@+id/btget" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/etnumber" android:layout_alignparentright="true" android:text="获取验证码" /> <button android:id="@+id/btregister" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:text="注册" /> </relativelayout>
3.mainactivity.java页面验证信息。
mainactivity.java页面:
package com.example.notetest; import org.json.jsonobject; import com.example.notetest.mycountdowntime; import android.app.activity; import android.os.bundle; import android.os.handler; import android.os.handler.callback; import android.os.message; import android.text.textutils; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.toast; import cn.smssdk.eventhandler; import cn.smssdk.smssdk; import cn.smssdk.utils.smslog; public class mainactivity extends activity implements onclicklistener, callback { private static string appkey= "13e9653a7ff00"; // 填写从短信sdk应用后台注册得到的appsecret private static string appsecret ="a881cac45bce10a59ccf22203062e763"; private edittext etphone; private edittext etnumber; private button btget; private button btregister; private string phone; private mycountdowntime mycountdowntime;// 用于验证码倒计时 private handler mhandler;//用于执行耗时操作 @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); smssdk.initsdk(this, appkey, appsecret, false); btget = (button) findviewbyid(r.id.btget); btregister = (button) findviewbyid(r.id.btregister); etphone = (edittext) findviewbyid(r.id.etphone); etnumber = (edittext) findviewbyid(r.id.etnumber); btget.setonclicklistener(this); btregister.setonclicklistener(this); initsdk(); } private void initsdk() { try { final handler handler = new handler(this); eventhandler eventhandler = new eventhandler() { public void afterevent(int event, int result, object data) { message msg = new message(); msg.arg1 = event; msg.arg2 = result; msg.obj = data; handler.sendmessage(msg); } }; smssdk.registereventhandler(eventhandler); // 注册短信回调 } catch (exception e) { e.printstacktrace(); } } @override public void onclick(view v) { switch (v.getid()) { case r.id.btget: phone = etphone.gettext().tostring().trim(); if (!textutils.isempty(phone)) { smssdk.getverificationcode("86", phone);// 获取短信 getcode(); } else { toast.maketext(mainactivity.this, "电话号码不能为空", toast.length_short).show(); } break; case r.id.btregister: string number = etnumber.gettext().tostring().trim(); if (!textutils.isempty(number)) { smssdk.submitverificationcode("86", phone, number);// 验证短信 } else { toast.maketext(mainactivity.this, "验证码不能为空", toast.length_short) .show(); return; } break; default: break; } } private void getcode() { mhandler = new handler(); mhandler.postdelayed(new runnable() { @override public void run() { starttimer(); } }, 2000); } /** * * @description: todo 发送成功后,开始倒计时 * */ private void starttimer() { if (null == mycountdowntime) { mycountdowntime = new mycountdowntime(60000, 1000, btget, "重新发送"); } mycountdowntime.start(); } /** * * @description: todo 一般发送失败时,需要重置button状态 */ private void canceltimer(){ if(null != mycountdowntime){ mycountdowntime.cancel(); mycountdowntime.onfinish(); } } protected void ondestroy() { // 销毁回调监听接口 smssdk.unregisteralleventhandler(); super.ondestroy(); } @override public boolean handlemessage(message msg) { int event = msg.arg1; int result = msg.arg2; object data = msg.obj; if (result == smssdk.result_complete) { // 回调完成 if (event == smssdk.event_submit_verification_code) { // 提交验证码成功 toast.maketext(mainactivity.this, "验证成功", toast.length_short) .show(); } else if (event == smssdk.event_get_verification_code) { // 获取验证码成功 toast.maketext(mainactivity.this, "获取验证码成功", toast.length_short) .show(); } else if (event == smssdk.event_get_supported_countries) { // 返回支持发送验证码的国家列表 } } else { 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 false; } } catch (exception e) { smslog.getinstance().w(e); } } return false; } }
4.新建mycountdowntime.java页面,用于获取验证码后按钮的倒计时。
mycountdowntime.java页面:
package com.example.notetest; import org.json.jsonobject; import com.example.notetest.mycountdowntime; import android.app.activity; import android.os.bundle; import android.os.handler; import android.os.handler.callback; import android.os.message; import android.text.textutils; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.toast; import cn.smssdk.eventhandler; import cn.smssdk.smssdk; import cn.smssdk.utils.smslog; public class mainactivity extends activity implements onclicklistener, callback { private static string appkey= "13e9653a7ff00"; // 填写从短信sdk应用后台注册得到的appsecret private static string appsecret ="a881cac45bce10a59ccf22203062e763"; private edittext etphone; private edittext etnumber; private button btget; private button btregister; private string phone; private mycountdowntime mycountdowntime;// 用于验证码倒计时 private handler mhandler;//用于执行耗时操作 @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); smssdk.initsdk(this, appkey, appsecret, false); btget = (button) findviewbyid(r.id.btget); btregister = (button) findviewbyid(r.id.btregister); etphone = (edittext) findviewbyid(r.id.etphone); etnumber = (edittext) findviewbyid(r.id.etnumber); btget.setonclicklistener(this); btregister.setonclicklistener(this); initsdk(); } private void initsdk() { try { final handler handler = new handler(this); eventhandler eventhandler = new eventhandler() { public void afterevent(int event, int result, object data) { message msg = new message(); msg.arg1 = event; msg.arg2 = result; msg.obj = data; handler.sendmessage(msg); } }; smssdk.registereventhandler(eventhandler); // 注册短信回调 } catch (exception e) { e.printstacktrace(); } } @override public void onclick(view v) { switch (v.getid()) { case r.id.btget: phone = etphone.gettext().tostring().trim(); if (!textutils.isempty(phone)) { smssdk.getverificationcode("86", phone);// 获取短信 getcode(); } else { toast.maketext(mainactivity.this, "电话号码不能为空", toast.length_short).show(); } break; case r.id.btregister: string number = etnumber.gettext().tostring().trim(); if (!textutils.isempty(number)) { smssdk.submitverificationcode("86", phone, number);// 验证短信 } else { toast.maketext(mainactivity.this, "验证码不能为空", toast.length_short) .show(); return; } break; default: break; } } private void getcode() { mhandler = new handler(); mhandler.postdelayed(new runnable() { @override public void run() { starttimer(); } }, 2000); } /** * * @description: todo 发送成功后,开始倒计时 * */ private void starttimer() { if (null == mycountdowntime) { mycountdowntime = new mycountdowntime(60000, 1000, btget, "重新发送"); } mycountdowntime.start(); } /** * * @description: todo 一般发送失败时,需要重置button状态 */ private void canceltimer(){ if(null != mycountdowntime){ mycountdowntime.cancel(); mycountdowntime.onfinish(); } } protected void ondestroy() { // 销毁回调监听接口 smssdk.unregisteralleventhandler(); super.ondestroy(); } @override public boolean handlemessage(message msg) { int event = msg.arg1; int result = msg.arg2; object data = msg.obj; if (result == smssdk.result_complete) { // 回调完成 if (event == smssdk.event_submit_verification_code) { // 提交验证码成功 toast.maketext(mainactivity.this, "验证成功", toast.length_short) .show(); } else if (event == smssdk.event_get_verification_code) { // 获取验证码成功 toast.maketext(mainactivity.this, "获取验证码成功", toast.length_short) .show(); } else if (event == smssdk.event_get_supported_countries) { // 返回支持发送验证码的国家列表 } } else { 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 false; } } catch (exception e) { smslog.getinstance().w(e); } } return false; } }
5.运行就可以显示目标效果了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 到底孕妇可以吃碧根果吗
下一篇: 上火吃紫薯效果怎么样