注册很多app或者网络账户的时候,经常需要手机获取验证码,来完成注册,那时年少,只是觉得手机获取验证码这件事儿很好玩,并没有关心太多,她是如何实现的,以及她背后的故事到底是什么样子的,现在小编接手的这个项目里面,就需要通过手机号进行注册,并且手机号发送相应的验证码,来完成注册,那么在一些应用app里面到底是如何实现点击按钮获取验证码,来完成注册这整个流程的呢?今天小编就以注册为例,和小伙伴们分享一下,如何通过手机号获取验证码来完成注册的一整套流程以及如何采用正则表达式来验证手机号码是否符合电信、移动、联通的规范。
首先我们需要做的第一步就是apiclient里面编写获取验证码的方法,具体代码如下:
<span >/** * 说明:获取验证码 * 作者:丁国华 * 时间:2015-8-27 下午5:47:36 */ public static String getValIDateCode(AppContext appContext,Map<String,Object> map) throws AppException { // 定义要访问的接口和要强转的实体 String valIDateUrl = _MakeURL(URLs.VALIDATE_CODE_URL,map); ValIDateCode valIDateCode = null; try { // 获取服务器端Json数据 String Json = http_get(appContext,valIDateUrl); // 解析为制定的实体对象 valIDateCode = (ValIDateCode) JsON.parSEObject(Json,ValIDateCode.class); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } // 返回验证码 return valIDateCode.getCode(); } </span>
第二步编写AppContent里面的接口方法,具体代码如下所示:
<span >/** * 说明:获取服务器验证码(不需要缓存) * 作者:丁国华 * @date 2015-8-28 上午9:07:14 */ public String getCode(Map<String,Object> map) throws AppException { String valIDateCode = ""; // 如果网络可连接且解析无误返回正确的验证码,否则返回空字符串 if (isNetworkConnected()) { try { valIDateCode = apiclient.getValIDateCode(this,map); } catch (AppException e) { if (valIDateCode == "") { throw e; } } } return valIDateCode; } </span>
第三步,在StringUtils里面编写验证号码是否是手机号的正则表达式,具体代码如下:
<span > /* 说明:移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188 * 联通:130、131、132、152、155、156、185、186 * 电信:133、153、180、189 * 总结起来就是第一位必定为1,第二位必定为3或5或8,其他位置的可以为0-9 * 验证号码 手机号 固话均可 * 作者:丁国华 * 2015年9月20日 13:52:35 */ public static boolean isPhoneNumberValID(String phoneNumber) { boolean isValID = false; String Expression = "((^(13|15|18)[0-9]{9}$)|(^0[1,2]{1}\d{1}-?\d{8}$)|(^0[3-9] {1}\d{2}-?\d{7,8}$)|(^0[1,2]{1}\d{1}-?\d{8}-(\d{1,4})$)|(^0[3-9]{1}\d{2}-? \d{7,8}-(\d{1,4})$))"; CharSequence inputStr = phoneNumber; Pattern pattern = Pattern.compile(Expression); Matcher matcher = pattern.matcher(inputStr); if (matcher.matches() ) { isValID = true; } return isValID; } </span>
第四步:编写xml里面的文件,具体代码如下所示:
<span ><?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" > <linearLayout > <button androID:ID="@+ID/register_back_login" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margintop="5dp" androID:background="@null" androID:drawable@R_419_6823@="@drawable/back" androID:padding@R_419_6823@="5dp" androID:text=" 登录" androID:textcolor="#FFFFFF" androID:textSize="18sp" /> <!-- 注册的布局 --> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margin="5dp" androID:layout_margintop="2dp" androID:layout_weight="1" androID:gravity="center" androID:padding@R_419_6823@="4dp" androID:text="注册" androID:textcolor="#FFFFFF" androID:textSize="20sp" /> <!-- 注册的布局 --> <TextVIEw androID:ID="@+ID/nickname_confirm" androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:layout_margintop="2dp" androID:gravity="center" androID:padding@R_419_6823@="60dp" androID:paddingRight="10dp" androID:textcolor="#FFFFFF" androID:textSize="20sp" /> </linearLayout> <relativeLayout androID:layout_wIDth="fill_parent" androID:layout_height="45dp" androID:minHeight="50.0dip" androID:padding@R_419_6823@="14.0dip" androID:paddingRight="12.0dip" > <ImageVIEw androID:layout_wIDth="23.0dip" androID:layout_height="23.0dip" androID:layout_centerVertical="true" androID:src="@drawable/user_picture" /> <EditText androID:ID="@+ID/et_register_username_ID" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margin@R_419_6823@="20dp" androID:background="@null" androID:hint="用户名/手机号" androID:padding@R_419_6823@="15dip" androID:paddingtop="8dp" androID:textcolorHint="#BEBEBE" androID:textSize="20sp" /> </relativeLayout> <VIEw /> <relativeLayout androID:layout_wIDth="fill_parent" androID:layout_height="45dp" androID:minHeight="50.0dip" androID:padding@R_419_6823@="14.0dip" androID:paddingRight="12.0dip" > <ImageVIEw androID:layout_wIDth="23.0dip" androID:layout_height="23.0dip" androID:layout_centerVertical="true" androID:src="@drawable/phone_picture" /> <EditText androID:ID="@+ID/et_register_code_ID" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margin@R_419_6823@="20dp" androID:background="@null" androID:hint="请输入验证码" androID:padding@R_419_6823@="15dip" androID:paddingtop="8dp" androID:textcolorHint="#BEBEBE" androID:textSize="20sp" /> <button androID:ID="@+ID/bt_getcode_ID" androID:layout_wIDth="120dp" androID:layout_height="35dp" androID:layout_margin@R_419_6823@="200dp" androID:layout_margintop="5dp" androID:background="@drawable/shape1" androID:text="获取验证码" androID:textcolor="#FFFFFF" androID:textSize="10sp" /> </relativeLayout> <VIEw /> <relativeLayout androID:layout_wIDth="fill_parent" androID:layout_height="45dp" androID:minHeight="50.0dip" androID:padding@R_419_6823@="14.0dip" androID:paddingRight="12.0dip" > <ImageVIEw androID:layout_wIDth="23.0dip" androID:layout_height="23.0dip" androID:layout_centerVertical="true" androID:src="@drawable/lock" /> <EditText androID:ID="@+ID/et_register_password_ID" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margin@R_419_6823@="20dp" androID:background="@null" androID:hint="请输入新密码" androID:padding@R_419_6823@="15dip" androID:paddingtop="8dp" androID:textcolorHint="#BEBEBE" androID:textSize="20sp" /> </relativeLayout> <VIEw /> <linearLayout androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:orIEntation="horizontal" > <!-- 小对勾的布局 --> <CheckBox androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margin="10dp" androID:layout_margin@R_419_6823@="-10dp" androID:scaleX="0.8" androID:scaleY="0.8" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center_vertical" androID:text="我同意" androID:textSize="18sp" /> <TextVIEw androID:ID="@+ID/user_protocol" androID:layout_wIDth="200dp" androID:layout_height="match_parent" androID:layout_gravity="center_vertical" androID:layout_margin@R_419_6823@="5dp" androID:gravity="center" androID:text="用户协议及隐私条款" androID:textcolor="#FE8B4A" androID:textSize="18sp" /> </linearLayout> <button androID:ID="@+ID/bt_register_ID" androID:layout_wIDth="245dp" androID:layout_height="45dp" androID:layout_gravity="center_horizontal" androID:layout_marginBottom="14dp" androID:layout_margin@R_419_6823@="15dp" androID:layout_marginRight="15dp" androID:layout_margintop="5dp" androID:background="@drawable/shape2" androID:gravity="center" androID:text="注 册" androID:textcolor="#FFFFFF" androID:textSize="15sp" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margin@R_419_6823@="80dp" androID:paddingtop="5dp" androID:text="您也可以直接登录" androID:textcolor="#BEBEBE" androID:textSize="20sp" /> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_margintop="10dp" androID:baselineAligned="false" androID:gravity="center" androID:orIEntation="horizontal" > <linearLayout androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_weight="1" androID:gravity="center" androID:orIEntation="vertical" > <button androID:layout_wIDth="60dp" androID:layout_height="60dp" androID:background="@drawable/weixin_login" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="微信登录" androID:textcolor="#BEBEBE" androID:textSize="20sp" /> </linearLayout> <linearLayout androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_weight="1" androID:gravity="center" androID:orIEntation="vertical" > <button androID:layout_wIDth="60dp" androID:layout_height="60dp" androID:background="@drawable/weibo_login" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="微博登录" androID:textcolor="#BEBEBE" androID:textSize="20sp" /> </linearLayout> <linearLayout androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_weight="1" androID:gravity="center" androID:orIEntation="vertical" > <button androID:layout_wIDth="60dp" androID:layout_height="60dp" androID:background="@drawable/qq_login" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="QQ登录" androID:textcolor="#BEBEBE" androID:textSize="20sp" /> </linearLayout> </linearLayout> </linearLayout></span>
第五步:编写java类Registeractivity里面的代码,具体如下所示:
<span >package com.jczb.car.ui; import java.lang.ref.WeakReference; import java.util.HashMap; import java.util.Map; import androID.app.Activity; import androID.content.Intent; import androID.os.Bundle; import androID.os.Handler; import androID.os.Message; import androID.vIEw.VIEw; import androID.vIEw.VIEw.OnClickListener; import androID.Widget.button; import androID.Widget.EditText; import androID.Widget.TextVIEw; import androID.Widget.Toast; import com.jczb.car.AppContext; import com.jczb.car.AppException; import com.jczb.car.R; import com.jczb.car.common.StringUtils; /** * 说明:注册功能页面 我们实现了取消线程的机制,从而保证它不会泄露 onDestroy()常常被用来在Activity推出前取消线程 * 作者: 吴利昌 * 时间: 2015-9-3上午9:19:15 */ public class Registeractivity extends Activity implements OnClickListener { // 声明用到的页面控件 private EditText etRegistername; private EditText etCode; private EditText etPassword; private button btCode; private button btRegister; private TextVIEw tvUserProtocol; private button btRegisterLoginBack; // 定义变量 private String username; private String passWord; public boolean isChange = false; private boolean tag = true; private int i = 60; Thread thread = null; /**客户端输入的验证码*/ private String valicationCode; /**服务器端获取的验证码*/ private static String serverValicationCode; /** 注册时所带的参数 */ private Map<String,Object> registerParams = new HashMap<String,Object>(); /** 获取验证码时所带的参数 */ private Map<String,Object> codeParams = new HashMap<String,Object>(); /** 注册是否成功 */ private String regisgerStatus; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.register); initVIEw(); } /** * 说明:初始化页面控件和事件 * 作者: 吴利昌 * 时间: 2015-9-3 上午9:23:42 */ public voID initVIEw() { // 初始化控件 etRegistername = (EditText) findVIEwByID(R.ID.et_register_username_ID); etCode = (EditText) findVIEwByID(R.ID.et_register_code_ID); etPassword = (EditText) findVIEwByID(R.ID.et_register_password_ID); btCode = (button) findVIEwByID(R.ID.bt_getcode_ID); btRegister = (button) findVIEwByID(R.ID.bt_register_ID); tvUserProtocol=(TextVIEw)findVIEwByID(R.ID.user_protocol); btRegisterLoginBack=(button)findVIEwByID(R.ID.register_back_login); // 初始化监听事件 btCode.setonClickListener(this); btRegister.setonClickListener(this); tvUserProtocol.setonClickListener(this); btRegisterLoginBack.setonClickListener(this); } private boolean isvalIDate() { // Todo auto-generated method stub // 获取控件输入的值 String username = etRegistername.getText().toString().trim(); if (StringUtils.isEmpty(username)) { Toast.makeText(this,"手机号不能为空",Toast.LENGTH_SHORT).show(); return false; } if (!StringUtils.isPhoneNumberValID(username)) { Toast.makeText(this,"手机号有误",Toast.LENGTH_SHORT).show(); return false; } return true; } @OverrIDe public voID onClick(VIEw v) { switch (v.getID()) { case R.ID.bt_getcode_ID: if(!isvalIDate()) break; btCode.setText("获取验证码"); btCode.setClickable(true); isChange = true; changeBtnGetCode(); getValIDateCode(); break; case R.ID.bt_register_ID: register(); break; case R.ID.user_protocol: Intent intentUserProtocol = new Intent(this,UserProtocolActivity.class); startActivity(intentUserProtocol); break; case R.ID.register_back_login: this.finish(); break; default: break; } } private voID changeBtnGetCode() { thread = new Thread() { @OverrIDe public voID run() { if (tag) { while (i > 0) { i--; if (Registeractivity.this == null) { break; } Registeractivity.this .runOnUiThread(new Runnable() { @OverrIDe public voID run() { btCode.setText("获取验证码(" + i + ")"); btCode .setClickable(false); } }); try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } } tag = false; } i = 60; tag = true; if (Registeractivity.this != null) { Registeractivity.this.runOnUiThread(new Runnable() { @OverrIDe public voID run() { btCode.setText("获取验证码"); btCode.setClickable(true); } }); } }; }; thread.start(); } /** * 说明:获取验证码 * * 作者: 吴利昌 * 时间: 2015-9-3 下午3:26:55 */ public boolean getValIDateCode() { String name = etRegistername.getText().toString().trim(); String code = etCode.getText().toString().trim(); if (name.equals("")) { Toast.makeText(this,"请输入用户名或手机号!",Toast.LENGTH_SHORT).show(); return false; }else { username = name; valicationCode = code; Thread codeThread = new Thread(codeRunnable); codeThread.start(); } return true; } /** * 说明:注册 * * 作者: 吴利昌 * 时间: 2015-9-3 下午3:27:23 */ public voID register() { // 1.首先判断输入的值是否有效 // 2.然后判断输入的验证码是否有效(防止没有点击获取验证码自己填的错误验证码) // 3.最后注册 if (isValID()) { if (valicationCode.equals(serverValicationCode)) { Thread thread = new Thread(sRunnable); thread.start(); }else { Toast.makeText(this,"输入的验证码不正确!",Toast.LENGTH_SHORT).show(); } } } //--------------------------------获取验证码线程处理过程---开始----------------------------- /** * 自定义一个静态的具有弱引用的Handler,解决内存泄漏的问题,本handler用来获取验证码 */ private static class CodeHandler extends Handler { // 持有对本外部类的弱引用 private final WeakReference<Registeractivity> mActivity; public CodeHandler(Registeractivity activity) { mActivity = new WeakReference<Registeractivity>(activity); } @OverrIDe public voID handleMessage(Message msg) { // 获取上下文对象 Registeractivity activity = mActivity.get(); if (activity != null) { switch (msg.what) { case 1: serverValicationCode = (String)msg.obj; //activity.etCode.setText(serverValicationCode); break; case -1: Toast.makeText(activity,"获取验证码失败!",Toast.LENGTH_SHORT).show(); break; case 0: Toast.makeText(activity,"哎呀,出错啦..",Toast.LENGTH_SHORT).show(); break; default: break; } } } } /**实例化自定义的handler*/ private final CodeHandler codeHandler = new CodeHandler(this); private String serverCode=null; /**定义获取验证码的子线程*/ private Runnable codeRunnable = new Runnable() { @OverrIDe public voID run() { Message msg = new Message(); Map<String,Object> map = new HashMap<String,Object>(); map.put("jbPhone",username); // 获取全局对象Application AppContext appContext = (AppContext) getApplication(); try { // 获取服务器数据 serverValicationCode = appContext.getCode(map); // 返回true则将消息的what值为1,为false则what为-1,异常为0 if (serverValicationCode.equals("")) { msg.what = -1; } else { msg.what = 1; msg.obj = serverValicationCode; } } catch (AppException e) { msg.what = 0; e.printstacktrace(); } codeHandler.sendMessage(msg); } };
//--------------------------------获取验证码线程处理过程----完成------------------------------
//--------------------------------注册线程处理过程--开始----------------------------------
/** * 自定义一个静态的具有弱引用的Handler,解决内存泄漏的问题,注册使用 */ private static class MyHandler extends Handler { // 持有对本外部类的弱引用 private final WeakReference<Registeractivity> mActivity; public MyHandler(Registeractivity activity) { mActivity = new WeakReference<Registeractivity>(activity); } @OverrIDe public voID handleMessage(Message msg) { // 获取上下文对象 Registeractivity activity = mActivity.get(); if (activity != null) { switch (msg.what) { case 1: Toast.makeText(activity,"注册成功!",Toast.LENGTH_SHORT).show(); activity.finish(); break; case -1: Toast.makeText(activity,"注册失败!",Toast.LENGTH_SHORT).show(); break; case -2: Toast.makeText(activity,"该号已经注册!",Toast.LENGTH_SHORT).show(); break; default: break; } } } } /**实例化自定义的handler*/ private final MyHandler mHandler = new MyHandler(this); /**自定义子线程*/ private Runnable sRunnable = new Runnable() { @OverrIDe public voID run() { Message msg = new Message(); // 获取全局对象Application AppContext appContext = (AppContext) getApplication(); try { // 获取服务器数据 regisgerStatus = appContext.register(registerParams); // 返回true则将消息的what值为1,为false则what为-1,异常为0 if (regisgerStatus.equals("true")) { msg.what = 1; msg.obj = regisgerStatus; } else if(regisgerStatus.equals("1")){ msg.what = -2; }else if(regisgerStatus.equals("false")){ msg.what = -1;} } catch (AppException e) { msg.what = 0; e.printstacktrace(); } mHandler.sendMessage(msg); } };
//--------------------------------注册线程处理过程---完成-----------------------------------
/** * 说明:注册之前判断数据是否为空 * * @return * 作者: 吴利昌 * 时间: 2015-9-3 下午3:29:04 */ public boolean isValID() { username = etRegistername.getText().toString().trim(); valicationCode = etCode.getText().toString().trim(); passWord = etPassword.getText().toString().trim(); if (username.equals("")) { Toast.makeText(this,"用户名不能为空!",Toast.LENGTH_SHORT).show(); return false; } if (valicationCode.equals("")) { Toast.makeText(this,"验证码不能为空!",Toast.LENGTH_SHORT).show(); return false; } if(!serverValicationCode.equals(valicationCode)) { Toast.makeText(this,"验证码错误",Toast.LENGTH_SHORT).show(); return false; } if (passWord.equals("")) { Toast.makeText(this,"密码不能为空!",Toast.LENGTH_SHORT).show(); return false; } else if (passWord.length() < 6) { Toast.makeText(this,"密码至少6位!",Toast.LENGTH_SHORT).show(); return false; } registerParams.put("username",username); registerParams.put("psd",passWord); return true; } } </span>
最后,我们来运行一下,看看我们的效果,由于小编的genymotion不知道为什么不能运行了,所以委屈小伙伴们一下,看不了动态图片了,不过并不影响,我们首先用一个号码好注册一下,如下图所示:
看一下手机收到的验证码:
最后来看一下,我们的注册:
小编寄语:该博文,小编主要简单的介绍了如何通过手机获取验证码来完成注册的功能,以及如何利用正则表达式来验证码手机号码是否符合移动、联通、电信。还是那句话对于小编来说,既是挑战更是机遇,因为知识都是相通的,再者来说,在小编的程序人生中,留下最珍贵的记忆,虽然以后小编不一定从事安卓这个行业,代码世界里,很多种事,有的甜蜜,有的温馨,有的婉转成歌,有的绵延不息,在这些故事里,我们唯一的共通之处就是,某年,某月,某个波澜不惊的日子里,曾经很爱很爱你!爱你--这段实习的日子里,安卓带给小编的种种的惊喜。
总结以上是内存溢出为你收集整理的Android如何通过手机获取验证码来完成注册功能全部内容,希望文章能够帮你解决Android如何通过手机获取验证码来完成注册功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)