获取手机注册状态是什么样的获取字符串中的字符

查看: 348|回复: 4
esp8266接收到来自手机端的字符串信息如何实现获取和判断来控制单片机的模块?
<td class="t_f" id="postmessage_单片机可以实现由串口调试助手发送字符串控制继电器的开关(已完成),但是通过esp8266接收来自手机端的数据时继电器无反应,串口调试助手上可以显示来自手机端的字符串信息,格式:+IPD,0,1,:00,CLOSED。&&0,1是数据长度,0是传过来的数据。而我在串口调试助手输入0或&+IPD,0,1,:00,CLOSED&都可以使继电器开启。而我问了一个有经验的人,他说esp8266接收到的数据就是串口传送的数据,两者是相通的,这我就不懂了。为什么我手机端发送的数据也被esp8266接收到了,继电器毫无反应。
之前我也有过这样的问题,你试试这样排除:
1. 我当时用的是51单片机开发板测试的,不知道你是不是也是用开发板,我这个板子上wifi模块上的tx和rx口只能发送或者通过串口助手回显字符串。
如果想要判断通过wifi接收的字符并做出反应,还是tmd得用单片机边上外接的txd和rxd端口来连我的esp8266,你看看问题是不是在这儿。
2.如果排除了第一个问题,你可以写一组回显代码,测试下你手机发送的字符能不能回显到串口助手上。
3.如果你连接了esp8266,那我建议字符识别还是尽量避免&AT&啊,&+&啊,或者数字,你试一下识别“K”字符,“G”字符之类的。
希望能帮到你吧。
回帖助人的奖励!
我也遇到这个问题 可以显示接受到这个命令但是不能对他进行字符串处理 所以我想用数组是不是好点 又解决吗
回帖助人的奖励!
1、51单片机实现由串口调试助手发送字符串控制继电器的开关可以说明你单片机程序串口收发功能是正常的,但是esp8266与51单片机的通信是有格式要求的,你的单片机程序要修改一下。
2、下面是P2口控制灯的51单片机程序,你可将灯换成继电器即可。
/********************************************************************/
#include &REGX51.H&
#define uint unsigned int
#define uchar unsigned char& &
sbit LED0=P2^0;&&& & & & //红灯
sbit LED1=P2^3;& & & & & & & & //黄灯
sbit LED2=P2^6;& & & && &&&//绿灯
/*****************相关变量**************/
uchar Receive,i,qj,yz,zz,& && && && && && && && && && &
uchar Recive_table[40];&&& & & & & & & & //用于接收wifi模块反馈到MCU上的数据&&
/*******************************************************************
名称:延时函数&&作用:毫秒级延时,微妙级延时函数,为数据收发完成作等待.......&&
********************************************************************/
void ms_delay(uint t)
& & & & uint i,j;&&
& & & & for(i=t;i&0;i--)& &
& & & & for(j=110;j&0;j--);
void us_delay(uchar t)
& & & & while(t--);
}& && && &
void Uart_Init()& & & & & & & & & & & & & & & & //使用定时器1作为波特率发生器(STC89C52、STC89C51、AT89C51等均可)
& & TMOD = 0x20;
& & SCON = 0x50;& & & & & & & &&&& & & & & & & & //设置串行方式
& & TH1 = 0xFD;& & & & & & & & & & & &&&& & & & & & & & //波特率9600
& & TL1 = TH1;
& & PCON = 0x00;
& & EA = 1;& & & & & & & & & & & & & & & & & & & & & & & & //总中断打开
& & ES = 1;& & & & & & & & & & & & & & & & & & & & & & & & //开串口中断&&
& & TR1 = 1;& & & & & & & & & & & & & & & & & & & & //启动定时器1
/********************************************************************
名称:串口发送函数&&功能:MCU向无线WIFI模块ESP8266发送数据&&
********************************************************************/
void Send_Uart(uchar value)
& & & & ES=0;&&& & & & & & & & & & & & & & & & & & & & & & & & //关闭串口中断&&
& & & & TI=0;& && & & & & & & & & & & & & & & & & & & & //清发送完毕中断请求标志位& &
& & & & SBUF= & & & & & & & & & & & & & & & & //发送&&
& & & & while(TI==0); & & & & & & & & & & & & & & & & //等待发送完毕& &
& & & & TI=0;& && & & & & & & & & & & & & & & & & & & & //清发送完毕中断请求标志位& &
& & & & ES=1;&&& & & & & & & & & & & & & & & & & & & & & & & & //允许串口中断&&
/********************************************************************
名称:WIFI模块设置函数&&作用: 启动模块,以便可以实现无线接入和控制&&
********************************************************************/
void ESP8266_Set(uchar *puf) & & & & // 数组指针*puf指向字符串数组& && && && && &
& & & & while(*puf!='\0')& & & & & & & & & & //遇到空格跳出循环&&
& & & & {& &
& & & & & & & & Send_Uart(*puf);&&& & & & & & & & //向WIFI模块发送控制指令。& &
& & & & & & & & us_delay(5);& &
& & & & & & & & puf++;& &
& & & & }&&
& & & & us_delay(5);&&
& & & & Send_Uart('\r'); & & & & & & & & & & & & //回车&&
& & & & us_delay(5);&&
& & & & Send_Uart('\n');& && & & & & & & & & & & & //换行&&
/********************************************************************
名称:WIFI模块发送函数&&作用: 启动模块,以便可以实现无线接入和控制&&
********************************************************************/
void ESP8266_Sent(uchar *puf)& && &// 数组指针*puf指向字符串数组& && && && && &
& && &ESP8266_Set(&AT+CIPSEND=0,26&);
& && &while(*puf!='\0')& & //遇到空格跳出循环
& && && &&&Send_Uart(*puf);& &//向WIFI模块发送控制指令。
& && && &&&us_delay(5);
& && && &&&puf++;& && &
& && &us_delay(5);
& && &Send_Uart('\n');& &//换行
& && &ms_delay(10);
/********************************************************************
名称:主函数&&作用:程序的执行入口&&
********************************************************************/
void main()
{&&& & & &
& & & & Uart_Init();& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & //波特率发生器
& & & & ms_delay(2000);
& & & & ESP8266_Set(&AT+CWMODE=2&); & & & & & & & & & & & & & & & & & & & & & & & & & & & & //设置路由器模式1 station,模式2 AP,模式3 station+AP混合模式& &
& & & & ms_delay(2000);
//& & & & ESP8266_Set(&AT+RST&); & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & //重新启动wifi模块& && && && &
//&&ms_delay(2000);
& & & & ESP8266_Set(&AT+CWSAP=\&wifi_yuan\&,\&\&,11,4&);&&& & & & //AT+CWSAP=&wifi_yuan&,&&,11,4&&设置模块SSID:WIFI, PWD:密码 及安全类型加密模式(WPA2-PSK)
& & & & ms_delay(2000);
& & & & ESP8266_Set(&AT+CIPMUX=1&);& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & //开启多连接模式,允许多个各客户端接入
& & & & ms_delay(2000);
& & & & ESP8266_Set(&AT+CIPSERVER=1,5000&);&&& & & & & & & & & & & & & & & & & & & & //启动TCP/IP 实现基于网络//控制 & & & & ESP8266_Set(&AT+CIPSERVER=1,5000&);&&
& & & & ms_delay(2000);
& & & & ESP8266_Set(&AT+CIPSTO=0&); & & & & & & & & & & & & & & & & & & & & & & & & & & & & //永远不超时
& & & & ES=1;& && && &&&& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & //允许串口中断 & & & & & & & &
& & & & qj=1;
& & & & zz=1;
& & & & yz=1;
& & & & ht=1;
& & & & LED0=1;
& & & & LED1=1;
& & & & LED2=1;
& & & & while(1)& &
& & & & {& &
& & & & & & & & if((Recive_table[0]=='+')&&(Recive_table[1]=='I')&&(Recive_table[2]=='P'))//MCU接收到的数据为+IPD时进入判断控制0\1来使小灯亮与灭& &
& & & & & & & & {& && && &&&
& & & & & & & & & & & & if((Recive_table[9]=='G')&&(Recive_table[10]=='P'))& && && && &
& & & & & & & & & & & & {& && &
& & & & & & & & & & & & & & & & if(Recive_table[15]=='0')& && &
& & & & & & & & & & & & & & & & {& && &
& & & & & & & & & & & & & & & & & & & & LED0=0; & & & & & & & & & & & & //红灯亮& & & & & & & & & & & && &
& & & & & & & & & & & & & & & && &&&ESP8266_Sent(&1号灯开灯miandeng&);
//wifi模块向pc端或手机端 发送&灯灭
& & & & & & & & & & & & & & & & }& && && && && && && && && && &&&
& & & & & & & & & & & & & & & & else
& & & & & & & & & & & & & & & & if (Recive_table[15]=='1')& && &
& & & & & & & & & & & & & & & & {& && && && &&&
& & & & & & & & & & & & & & & & & & & & LED0=1; & & & & & & & & & & & & //红灯灭
& & & & & & & & & & & & & & & && &&&ESP8266_Sent(&1号灯关灯liangdeng&);& &&&
//wifi模块向pc端或手机端 发送&灯亮&&&
& & & & & & & & & & & & & & & & }
& & & & & & & & & & & & & & & & else
& & & & & & & & & & & & & & & & if (Recive_table[15]=='2')& && &
& & & & & & & & & & & & & & & & {& && && && &&&
& && && && && && &&&LED1=0;& && && && &//黄灯亮&&
& & & & & & & & & & & & & & & & & & & & ESP8266_Sent(&2号灯开灯miandeng&);& && && &
& & & & & & & & & & & & & & & & }
& & & & & & & & & & & & & & & & else
& & & & & & & & & & & & & & & & if (Recive_table[15]=='3')& && &
& & & & & & & & & & & & & & & & {& && && && &&&
& & & && && && && && &&&LED1=1; & & & & & & & && & //黄灯灭
& & & & & & & & & & & & & & & & & & & & ESP8266_Sent(&2号灯关灯miandeng&);
& & & & & & & & & & & & & & & & }
& & & & & & & & & & & & & & & & else
& & & & & & & & & & & & & & & & if (Recive_table[15]=='4')& && &
& & & & & & & & & & & & & & & & {& && && && &&&
& & & && && && && && &&&LED2=0;& && & & & & & & & & & //绿灯亮
& & & & & & & & & & & & & & & & & & & & ESP8266_Sent(&3号灯开灯miandeng&);
& & & & & & & & & & & & & & & & }&&
& & & & & & & & & & & & & & & & else
& & & & & & & & & & & & & & & & if (Recive_table[15]=='5')& && &
& & & & & & & & & & & & & & & & {& && && && &&&
& & & && && && && && &&&LED2=1;& && & & & & & & & & & //绿灯灭
& & & & & & & & & & & & & & & & & & & & ESP8266_Sent(&3号灯关灯miandeng&);
& & & & & & & & & & & & & & & & }&&& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &&&& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & && &
& & & & & & & & & & & & }&&
& & & & & & & & }
&&& & & & }& &
/*********************************************************************&&
名称:串行通讯中断&&作用:发送或接收结束后进入该函数,对相应的标志位软件清0,实现模块对数据正常的收发。&&
********************************************************************/
void Uart_Interrupt() interrupt 4& && && &
& & & & static uchar i=0;&&
& & & & if(RI==1)&&
& & & & {& &
& & & & & & & & RI=0;& &
& & & & & & & & Receive=SBUF;& && &&&& & & & & & & & & & & & & & & & & & & & & & & & //MCU接收wifi模块反馈回来的数据& && &
& & & & & & & & Recive_table[i]=R& &
& & & & & & & & if((Recive_table[i]=='\n'))
& & & & & & & & {& & & & & & & && && &
& & & & & & & & & & & & i=0;
& & & & & & & & }& &
& & & & & & & & else i++;&&& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & //遇到换行 重新装值&&
& & & & }& &
& & & & else TI=0;& &
Powered by博主最新文章
博主热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)Android如何通过手机获取验证码来完成注册功能_Android
作者:用户
本文讲的是Android如何通过手机获取验证码来完成注册功能_Android,
注册很多app或者网络账户的时候,经常需要手机获取验证码,来完成注册,那时年少,只是觉得手机获取验证码这件事儿很好玩,并没有关心太多,她是如何实现的,以及她背后的故事到底是什么样子的,现在小编接手的这个项目里面,就需要通过手机号进行注册,
注册很多app或者网络账户的时候,经常需要手机获取验证码,来完成注册,那时年少,只是觉得手机获取验证码这件事儿很好玩,并没有关心太多,她是如何实现的,以及她背后的故事到底是什么样子的,现在小编接手的这个项目里面,就需要通过手机号进行注册,并且手机号发送相应的验证码,来完成注册,那么在一些应用app里面到底是如何实现点击按钮获取验证码,来完成注册这整个流程的呢?今天小编就以注册为例,和小伙伴们分享一下,如何通过手机号获取验证码来完成注册的一整套流程以及如何采用正则表达式来验证手机号码是否符合电信、移动、联通的规范。
首先我们需要做的第一步就是ApiClient里面编写获取验证码的方法,具体代码如下:
&span style="font-size:18"&/**
* 说明:获取验证码
* 作者:丁国华
* 时间: 下午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 =
// 获取服务器端Json数据
String json = http_get(appContext, validateUrl);
// 解析为制定的实体对象
validateCode = (ValidateCode) JSON.parseObject(json,
ValidateCode.class);
} catch (Exception e) {
if (e instanceof AppException)
throw (AppException)
throw AppException.network(e);
// 返回验证码
return validateCode.getCode();
第二步编写AppContent里面的接口方法,具体代码如下所示:
&span style="font-size:18"&/**
* 说明:获取服务器验证码(不需要缓存)
* 作者:丁国华
上午9:07:14
public String getCode(Map&String, Object& map) throws AppException {
String validateCode = "";
// 如果网络可连接且解析无误返回正确的验证码,否则返回空字符串
if (isNetworkConnected()) {
validateCode = ApiClient.getValidateCode(this, map);
} catch (AppException e) {
if (validateCode == "") {
return validateC
第三步,在StringUtils里面编写验证号码是否是手机号的正则表达式,具体代码如下:
&span style="font-size:18"& /* 说明:移动: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
* 验证号码 手机号 固话均可
* 作者:丁国华
* 日 13:52:35
public static boolean isPhoneNumberValid(String phoneNumber) {
boolean isValid =
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 = phoneN
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches() ) {
return isV
第四步:编写xml里面的文件,具体代码如下所示:
&span style="font-size:18"&&?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 style="@style/top_title_style" &
android:id="@+id/register_back_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@null"
android:drawableLeft="@drawable/back"
android:paddingLeft="5dp"
android:text=" 登录"
android:textColor="#FFFFFF"
android:textSize="18sp" /&
&!-- 注册的布局 --&
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:paddingLeft="4dp"
android:text="注册"
android:textColor="#FFFFFF"
android:textSize="20sp" /&
&!-- 注册的布局 --&
android:id="@+id/nickname_confirm"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:gravity="center"
android:paddingLeft="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:paddingLeft="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" /&
android:id="@+id/et_register_username_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:background="@null"
android:hint="用户名/手机号"
android:paddingLeft="15dip"
android:paddingTop="8dp"
android:textColorHint="#BEBEBE"
android:textSize="20sp" /&
&/RelativeLayout&
&View style="@style/PersonalLine" /&
&RelativeLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:minHeight="50.0dip"
android:paddingLeft="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" /&
android:id="@+id/et_register_code_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:background="@null"
android:hint="请输入验证码"
android:paddingLeft="15dip"
android:paddingTop="8dp"
android:textColorHint="#BEBEBE"
android:textSize="20sp" /&
android:id="@+id/bt_getcode_id"
android:layout_width="120dp"
android:layout_height="35dp"
android:layout_marginLeft="200dp"
android:layout_marginTop="5dp"
android:background="@drawable/shape1"
android:text="获取验证码"
android:textColor="#FFFFFF"
android:textSize="10sp" /&
&/RelativeLayout&
&View style="@style/PersonalLine" /&
&RelativeLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:minHeight="50.0dip"
android:paddingLeft="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" /&
android:id="@+id/et_register_password_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:background="@null"
android:hint="请输入新密码"
android:paddingLeft="15dip"
android:paddingTop="8dp"
android:textColorHint="#BEBEBE"
android:textSize="20sp" /&
&/RelativeLayout&
&View style="@style/PersonalLine" /&
&LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" &
&!-- 小对勾的布局 --&
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginLeft="-10dp"
android:scaleX="0.8"
android:scaleY="0.8" /&
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="我同意"
android:textSize="18sp" /&
android:id="@+id/user_protocol"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:gravity="center"
android:text="用户协议及隐私条款"
android:textColor="#FE8B4A"
android:textSize="18sp" /&
&/LinearLayout&
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_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:background="@drawable/shape2"
android:gravity="center"
android:text="注 册"
android:textColor="#FFFFFF"
android:textSize="15sp" /&
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="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" &
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/weixin_login" /&
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" &
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/weibo_login" /&
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" &
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/qq_login" /&
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 style="font-size:18"&package com.jczb.car.
import java.lang.ref.WeakR
import java.util.HashM
import java.util.M
import android.app.A
import android.content.I
import android.os.B
import android.os.H
import android.os.M
import android.view.V
import android.view.View.OnClickL
import android.widget.B
import android.widget.EditT
import android.widget.TextV
import android.widget.T
import com.jczb.car.AppC
import com.jczb.car.AppE
import com.jczb.car.R;
import com.jczb.car.common.StringU
* 说明:注册功能页面 我们实现了取消线程的机制,从而保证它不会泄露 onDestroy()常常被用来在Activity推出前取消线程
* 作者: 吴利昌
* 时间: 上午9:19:15
public class RegisterActivity extends Activity implements OnClickListener {
// 声明用到的页面控件
private EditText etRegisterN
private EditText etC
private EditText etP
private Button btC
private Button btR
private TextView tvUserP
private Button btRegisterLoginB
// 定义变量
private String userN
private String passW
public boolean isChange =
private boolean tag =
private int i = 60;
Thread thread =
/**客户端输入的验证码*/
private String valicationC
/**服务器端获取的验证码*/
private static String serverValicationC
/** 注册时所带的参数 */
private Map&String, Object& registerParams = new HashMap&String, Object&();
/** 获取验证码时所带的参数 */
private Map&String, Object& codeParams = new HashMap&String, Object&();
/** 注册是否成功 */
private String regisgerS
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
initView();
* 说明:初始化页面控件和事件
* 作者: 吴利昌
上午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();
if (!StringUtils.isPhoneNumberValid(userName)) {
Toast.makeText(this, "手机号有误", Toast.LENGTH_SHORT).show();
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_getcode_id:
if(!isvalidate())
btCode.setText("获取验证码");
btCode.setClickable(true);
isChange =
changeBtnGetCode();
getValidateCode();
case R.id.bt_register_id:
register();
case R.id.user_protocol:
Intent intentUserProtocol = new Intent(this,UserProtocolActivity.class);
startActivity(intentUserProtocol);
case R.id.register_back_login:
this.finish();
private void changeBtnGetCode() {
thread = new Thread() {
public void run() {
if (tag) {
while (i & 0) {
if (RegisterActivity.this == null) {
RegisterActivity.this
.runOnUiThread(new Runnable() {
public void run() {
btCode.setText("获取验证码("
+ i + ")");
.setClickable(false);
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
if (RegisterActivity.this != null) {
RegisterActivity.this.runOnUiThread(new Runnable() {
public void run() {
btCode.setText("获取验证码");
btCode.setClickable(true);
thread.start();
* 说明:获取验证码
* 作者: 吴利昌
下午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();
userName =
valicationCode =
Thread codeThread = new Thread(codeRunnable);
codeThread.start();
* 说明:注册
* 作者: 吴利昌
下午3:27:23
public void register() {
// 1.首先判断输入的值是否有效
// 2.然后判断输入的验证码是否有效(防止没有点击获取验证码自己填的错误验证码)
// 3.最后注册
if (isValid()) {
if (valicationCode.equals(serverValicationCode)) {
Thread thread = new Thread(sRunnable);
thread.start();
Toast.makeText(this, "输入的验证码不正确!", Toast.LENGTH_SHORT).show();
//--------------------------------获取验证码线程处理过程---开始-----------------------------
* 自定义一个静态的具有弱引用的Handler,解决内存泄漏的问题,本handler用来获取验证码
private static class CodeHandler extends Handler {
// 持有对本外部类的弱引用
private final WeakReference&RegisterActivity& mA
public CodeHandler(RegisterActivity activity) {
mActivity = new WeakReference&RegisterActivity&(activity);
public void handleMessage(Message msg) {
// 获取上下文对象
RegisterActivity activity = mActivity.get();
if (activity != null) {
switch (msg.what) {
serverValicationCode = (String)msg.
//activity.etCode.setText(serverValicationCode);
Toast.makeText(activity, "获取验证码失败!", Toast.LENGTH_SHORT).show();
Toast.makeText(activity, "哎呀,出错啦..", Toast.LENGTH_SHORT).show();
/**实例化自定义的handler*/
private final CodeHandler codeHandler = new CodeHandler(this);
private String serverCode=
/**定义获取验证码的子线程*/
private Runnable codeRunnable = new Runnable() {
public void run() {
Message msg = new Message();
Map&String, Object& map = new HashMap&String, Object&();
map.put("jbPhone", userName);
// 获取全局对象Application
AppContext appContext = (AppContext) getApplication();
// 获取服务器数据
serverValicationCode = appContext.getCode(map);
// 返回true则将消息的what值为1,为false则what为-1,异常为0
if (serverValicationCode.equals("")) {
msg.what = -1;
msg.what = 1;
msg.obj = serverValicationC
} catch (AppException e) {
msg.what = 0;
e.printStackTrace();
codeHandler.sendMessage(msg);
//--------------------------------获取验证码线程处理过程----完成------------------------------
//--------------------------------注册线程处理过程--开始----------------------------------
* 自定义一个静态的具有弱引用的Handler,解决内存泄漏的问题,注册使用
private static class MyHandler extends Handler {
// 持有对本外部类的弱引用
private final WeakReference&RegisterActivity& mA
public MyHandler(RegisterActivity activity) {
mActivity = new WeakReference&RegisterActivity&(activity);
public void handleMessage(Message msg) {
// 获取上下文对象
RegisterActivity activity = mActivity.get();
if (activity != null) {
switch (msg.what) {
Toast.makeText(activity, "注册成功!", Toast.LENGTH_SHORT).show();
activity.finish();
Toast.makeText(activity, "注册失败!", Toast.LENGTH_SHORT).show();
Toast.makeText(activity, "该号已经注册!", Toast.LENGTH_SHORT).show();
Toast.makeText(activity, "哎呀,出错啦..", Toast.LENGTH_SHORT).show();
/**实例化自定义的handler*/
private final MyHandler mHandler = new MyHandler(this);
/**自定义子线程*/
private Runnable sRunnable = new Runnable() {
public void run() {
Message msg = new Message();
// 获取全局对象Application
AppContext appContext = (AppContext) getApplication();
// 获取服务器数据
regisgerStatus = appContext.register(registerParams);
// 返回true则将消息的what值为1,为false则what为-1,异常为0
if (regisgerStatus.equals("true")) {
msg.what = 1;
msg.obj = regisgerS
} 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);
//--------------------------------注册线程处理过程---完成-----------------------------------
* 说明:注册之前判断数据是否为空
* 作者: 吴利昌
下午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();
if (valicationCode.equals("")) {
Toast.makeText(this, "验证码不能为空!", Toast.LENGTH_SHORT).show();
if(!serverValicationCode.equals(valicationCode))
Toast.makeText(this, "验证码错误", Toast.LENGTH_SHORT).show();
if (passWord.equals("")) {
Toast.makeText(this, "密码不能为空!", Toast.LENGTH_SHORT).show();
} else if (passWord.length() & 6) {
Toast.makeText(this, "密码至少6位!", Toast.LENGTH_SHORT).show();
registerParams.put("username", userName);
registerParams.put("psd", passWord);
最后,我们来运行一下,看看我们的效果,由于小编的genymotion不知道为什么不能运行了,所以委屈小伙伴们一下,看不了动态图片了,不过并不影响,我们首先用一个号码好注册一下,如下图所示:
看一下手机收到的验证码:
最后来看一下,我们的注册:
小编寄语:该博文,小编主要简单的介绍了如何通过手机获取验证码来完成注册的功能,以及如何利用正则表达式来验证码手机号码是否符合移动、联通、电信。还是那句话对于小编来说,既是挑战更是机遇,因为知识都是相通的,再者来说,在小编的程序人生中,留下最珍贵的记忆,虽然以后小编不一定从事安卓这个行业,代码世界里,很多种事,有的甜蜜,有的温馨,有的婉转成歌,有的绵延不息,在这些故事里,我们唯一的共通之处就是,某年,某月,某个波澜不惊的日子里,曾经很爱很爱你!爱你--这段实习的日子里,安卓带给小编的种种的惊喜。
以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索Android验证码
Android获取验证码
Android短信验证码
android手机验证码
完成拼图以获取验证码、android 获取验证码、请点击验证码完成发帖、验证码拼图如何完成、android 验证码控件,以便于您获取更多的相关知识。
弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率
40+云计算产品,6个月免费体验
稳定可靠、可弹性伸缩的在线数据库服务,全球最受欢迎的开源数据库之一
云服务器9.9元/月,大学必备
云栖社区(yq.aliyun.com)为您免费提供相关信息,包括
,所有相关内容均不代表云栖社区的意见!

我要回帖

更多关于 获取字符串中的字符 的文章

 

随机推荐