安卓注册手机短信验证码验证的案例-02
来源:互联网 发布:网络电视机哪个性价比 编辑:程序博客网 时间:2024/05/20 05:28
本部分我们将讲述如何实现手机注册验证码的代码
首先我们得有一个验证码的界面如下图所示:
代码如下:
<?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" > <com.vann.oschina.view.TitleBarView android:id="@+id/title_bar" android:layout_height="60dp" android:layout_width="match_parent"/> <LinearLayout android:id="@+id/ll_phone" android:layout_height="60dp" android:layout_width="match_parent" android:layout_margin="20dp" android:orientation="horizontal" android:background="@drawable/register_phone_bg"> <TextView android:layout_height="60dp" android:layout_width="100dp" android:gravity="center" android:text="@string/tv_country" android:textColor="@color/blue" android:textSize="16sp"/> <View android:layout_height="match_parent" android:layout_width="1dp" android:background="@color/devide_line"/> <EditText android:id="@+id/et_phoneNumber" android:layout_height="56dp" android:layout_width="match_parent" android:layout_marginRight="2dp" android:layout_marginTop="2dp" android:layout_marginBottom="2dp" android:hint="@string/et_phoneNumber_hint" android:background="@android:color/white" android:inputType="phone" android:padding="5dp"/> </LinearLayout> <Button android:id="@+id/code_next" android:textColor="@color/whites" android:layout_height="60dp" android:layout_width="match_parent" android:layout_margin="20dp" android:text="@string/btn_code" android:textSize="18sp" android:background="@color/disabel"/> <LinearLayout android:id="@+id/code_phone" android:layout_height="60dp" android:layout_width="match_parent" android:layout_margin="20dp" android:orientation="horizontal" android:background="@drawable/register_phone_bg"> <EditText android:id="@+id/et_codeNumber" android:layout_height="56dp" android:layout_width="match_parent" android:layout_marginRight="2dp" android:layout_marginTop="2dp" android:layout_marginBottom="2dp" android:hint="@string/et_codeNumber_hint" android:background="@android:color/white" android:inputType="phone" android:padding="5dp"/> </LinearLayout> <Button android:id="@+id/btn_next" android:layout_height="60dp" android:textColor="@color/whites" android:layout_width="match_parent" android:layout_margin="20dp" android:text="@string/btn_next" android:textSize="18sp" android:background="@color/disabel"/> <LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_margin="20dp" android:orientation="horizontal"> <CheckBox android:id="@+id/ck_agreen" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/checkbox" android:button="@null" android:checked="true"/> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center_vertical" android:text="@string/tv_xieyi" android:layout_marginLeft="5dp" android:textSize="16sp"/> <com.vann.oschina.view.TextURLView android:id="@+id/tv_url" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center_vertical"/> </LinearLayout></LinearLayout>
接下来我们完成 RegisterPhoneActivity.java 的代码值得注意的是由于sdk需要key和密钥所以各位客官需要在mob官网注册帐号获取
并且sdk是免费的但是有限制为20条/天
<pre name="code" class="java">package com.vann.oschina.activity;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.text.TextUtils;import android.util.Log;import android.view.Gravity;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.FrameLayout;import android.widget.ProgressBar;import android.widget.Toast;import android.widget.FrameLayout.LayoutParams;import com.vann.oschina.R;import com.vann.oschina.view.TextURLView;import com.vann.oschina.view.TitleBarView;import cn.smssdk.EventHandler;import cn.smssdk.SMSSDK;public class RegisterPhoneActivity extends Activity implements OnClickListener {private TitleBarView mTitleBarView;private TextURLView mTextViewURL;private Button next;//声明butto下一步private Button code;//声明button获取验证码private EditText phone;//声明EditText 手机号码输入框private EditText finalcode;//声明EditText 验证码输入框int i = 30; // 声明 int i= 30 后面用于验证码倒计时private EventHandler eventHandler = new MyHandler();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_register_phone);//设置界面xmlSMSSDK.initSDK(this, "你的key", "你的screct");//sms短信验证码sdkSMSSDK.registerEventHandler(eventHandler);//方法回调findView();//调用findview 方法绑定控件initTitleView();//调用initTitleView方法initTvUrl();//调用initTvUrl方法}private void findView(){mTitleBarView=(TitleBarView) findViewById(R.id.title_bar);mTextViewURL=(TextURLView) findViewById(R.id.tv_url);next=(Button) findViewById(R.id.btn_next);code=(Button) findViewById(R.id.code_next);phone=(EditText) findViewById(R.id.et_phoneNumber);finalcode=(EditText) findViewById(R.id.et_codeNumber);next.setOnClickListener(this);//给下一步按钮设置监听code.setOnClickListener(this);//给获取验证码按钮设置监听}//方法回调详细解释请查看官网:http://wiki.mob.com/android-%E7%9F%AD%E4%BF%A1sdk%E6%93%8D%E4%BD%9C%E5%9B%9E%E8%B0%83/private class MyHandler extends EventHandler {@Overridepublic void onRegister() {super.onRegister();}@Overridepublic void beforeEvent(int event, Object data) { super.beforeEvent(event, data);}@Overridepublic void afterEvent(int event, int result, Object data) {Message msg = new Message();msg.arg1 = event;msg.arg2 = result;msg.obj = data;handler.sendMessage(msg);super.afterEvent(event, result, data);}@Overridepublic void onUnregister() {super.onUnregister();}} Handler handler = new Handler() {public void handleMessage(Message msg) {if (msg.what == -9) {code.setText("重新发送(" + i + ")");} else if (msg.what == -8) {code.setText("获取验证码");code.setClickable(true);i = 30;} else {int event = msg.arg1;int result = msg.arg2;Object data = msg.obj;Log.e("event", "event=" + event);if (result == SMSSDK.RESULT_COMPLETE) {// 短信注册成功后,返回下一个界面,然后提示if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {// 提交验证码成功Toast.makeText(getApplicationContext(), "验证成功",Toast.LENGTH_SHORT).show();//消息提示Intent intent = new Intent(RegisterPhoneActivity.this,RegisterInfoActivity.class);//跳转activityintent.putExtra("phone",phone.getText().toString());//将phone值放进phone里以便传送至下一个activity中startActivity(intent);finish();//设置无法返回本界面} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {Toast.makeText(getApplicationContext(), "验证码已经发送",Toast.LENGTH_SHORT).show();} else {((Throwable) data).printStackTrace();}}}}};public void onClick(View v) {String phoneNums = phone.getText().toString();switch (v.getId()) {case R.id.code_next:// 1. 通过规则判断手机号if (!judgePhoneNums(phoneNums)) {return;} // 2. 通过sdk发送短信验证SMSSDK.getVerificationCode("86", phoneNums);// 3. 把按钮变成不可点击,并且显示倒计时(正在获取) code.setClickable(false);code.setText("重新发送(" + i + ")");new Thread(new Runnable() {@Overridepublic void run() {for (; i > 0; i--) {handler.sendEmptyMessage(-9);if (i <= 0) {break;}try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}handler.sendEmptyMessage(-8);}}).start();break;case R.id.btn_next:SMSSDK.submitVerificationCode("86", phoneNums, finalcode.getText().toString());createProgressBar();break;}}/** * 判断手机号码是否合理 * * @param phoneNums */private boolean judgePhoneNums(String phoneNums) {if (isMatchLength(phoneNums, 11)&& isMobileNO(phoneNums)) {return true;}Toast.makeText(this, "手机号码输入有误!",Toast.LENGTH_SHORT).show();return false;}/** * 判断一个字符串的位数 * @param str * @param length * @return */public static boolean isMatchLength(String str, int length) {if (str.isEmpty()) {return false;} else {return str.length() == length ? true : false;}}/** * 验证手机格式 */public static boolean isMobileNO(String mobileNums) {/* * 移动: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、(1349卫通) * 总结起来就是第一位必定为1,第二位必定为3或5或8,其他位置的可以为0-9 */String telRegex = "[1][358]\\d{9}";// "[1]"代表第1位为数字1,"[358]"代表第二位可以为3、5、8中的一个,"\\d{9}"代表后面是可以是0~9的数字,有9位。if (TextUtils.isEmpty(mobileNums))return false;elsereturn mobileNums.matches(telRegex);}/** * progressbar */private void createProgressBar() {FrameLayout layout = (FrameLayout) findViewById(android.R.id.content);FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);layoutParams.gravity = Gravity.CENTER;ProgressBar mProBar = new ProgressBar(this);mProBar.setLayoutParams(layoutParams);mProBar.setVisibility(View.VISIBLE);layout.addView(mProBar);}@Overrideprotected void onDestroy() {SMSSDK.unregisterAllEventHandler();super.onDestroy();}private void initTitleView(){mTitleBarView.setCommonTitle(View.VISIBLE, View.VISIBLE, View.GONE, View.GONE);mTitleBarView.setBtnLeft(R.drawable.boss_unipay_icon_back, R.string.back);mTitleBarView.setTitleText(R.string.title_phoneNumber);mTitleBarView.setBtnLeftOnclickListener(new OnClickListener() {@Overridepublic void onClick(View v) {finish();}});}private void initTvUrl(){mTextViewURL.setText(R.string.tv_xieyi_url);}}
Demo为eclipse版
所以eclipse和andriod studio都可以用
点击下载
1 0
- 安卓注册手机短信验证码验证的案例-02
- 安卓注册手机短信验证码验证的案例-01
- 注册手机短信验证
- 手机短信验证码验证
- 手机短信验证码注册-短信平台验证码开发6
- 手机短信验证码的实现
- 手机短信验证码的实现
- 手机短信验证码的定时器
- 手机短信验证的获取
- 手机短信验证码定时器
- 手机短信验证码实现
- Java用户注册手机短信验证码校验功能实现
- 手机短信验证
- 手机短信验证
- 关于手机短信验证的开发
- Session技术案例-用户注册验证码
- 随机生成手机短信验证码
- 手机短信验证码交互概念
- Makefile_文件制作浅谈
- git 命令小结
- UVA - 1103 Ancient Messages dfs+stl
- web.xml配置错误异常页面不起作用
- CMake使用
- 安卓注册手机短信验证码验证的案例-02
- iOS 多线程 NSthread的简单使用——iOS 编码复习(六)(多线程4)
- 编程学习心得
- 是跨平台的开发 Qt Creator
- 虚函数实现机制
- 【java排错】java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
- JAVA序列化与反序列化三种格式存取(默认格式、XML格式、JSON格式)
- UIviewcontroller与UIview
- Caffe 代码解读之全连接层 inner product layer