在Android开发之MOB短信验证SDK的使用(一)中我已经介绍了如何快速集成MOB短信验证SDK,那今天我们讲什么呢?我们今天讲一下如何不使用SDK中的GUI界面。对!我们今天来学习怎么自定义GUI,说的直白点就是自定义界面并把开发中所要用的接口绑定到自定义界面的控件上。

首先,我们还是看一下SDK中为我们提供了那些静态方法(这说明了我们只要类点方法名,对应传参就行了)?

1初始化接口

1.1 initSDK(Context context, String appkey, String appSecrect) 初始化SDK,单例,可以多次调用;任何方法调用前,必须先初始化 屌丝我放在了application中 全局性质 (初始化有用)

1.2 registerEventHandler(EventHandler handler)注册回调接口 这个我放在了要用的activity的oncreate()方法里 注意这是回掉,如果不用handler的消息机制是会报错的 它会提示你create一个handler去解决这个问题 后面会讲到土司不能在这里面写 (注测要用的)

1.3 unregisterEventHandler(EventHandler handler)注销回调接口 都说注销 意思就是destory() 我放在activity的onDestory()里 (注销也要用的 不然会内存溢出 OOM了)

2.短信验证码接口

2.1getSupportedCountries()获取短信目前支持的国家列表 (一般我们做的是国内项目,外国项目很少会用到这个 默认直接传字符串“ 86” 这里指的是中国 基本不需要用 直接传值,看需求吧)

2.2getVerificationCode(String country, String phone)请求获取短信验证码(这个也要用的 我们做短信验证不可缺少啊 )

2.3submitVerificationCode(String country, String phone, String code)提交短信验证码 (这个就是提交验证码并验证 我这里用的是MOB的智能验证,所以在客户端没显示 有用的)

3.用户接口

3.1submitUserInfo(String uid, String nickname, String avatar, String country, String phone)提交用户信息 (有用的 就是提交到自己的服务器上)

3.2getNewFriendsCount()获取应用内新增加的好友数(感觉没啥用 看需求吧)

3.3getFriendsInApp()获取应用内的好友列表 (上同)

4.语音验证接口

4.1getVoiceVerifyCode(String phone , String country)请求语音验证码2.0.0以下版本 (高科技的样子,我们初次注册点餐软件时好像用过,没啥用 看需求)

4.2getVoiceVerifyCode(String country,String phone)请求语音验证码2.0.0与之后的 (上同)


这些接口 从上往下的顺序去使用就行了

基本思路就是

1.初始化接口 就是基本的接口进行配置

2.我要做什么功能呢?对,我们要短信验证啊 那就调短信请求接口

3.我们得到短信了,那我们要干啥?验证呀!

4.验证完,我们这个用户的信息我们是不是要保存下来啊 那就提交服务器咯

接下来我们看代码

看着上面的思路:

1.我们要初始化吧,势必要初始化接口那就application里初始化咯

<span style="font-size:18px;">package com.example.boom.messageproject.application;

import android.app.Application;

import cn.smssdk.SMSSDK;

/**
* Created by Boom on 2016/8/1.
*/
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
SMSSDK.initSDK(this, "你的key", "你的密钥");
}
}
</span>
2.那我们要用这个东西吧,就要注册吧!

<span style="font-size:18px;">    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.regeister);
<span style="color:#6600cc;"> //注册短信回调
SMSSDK.registerEventHandler(new EventHandler() {
@Override
public void afterEvent(int event, int result, Object data) {
Message msg = Message.obtain();
msg.arg1 = event;
msg.arg2 = result;
msg.obj = data;
handler.sendMessage(msg);
}
});</span>
findViewId();
init();
}</span>
3.注册完,我们千万不要忘记要注销 那么我们现在就写好,以免之后忘了

<span style="font-size:18px;">   @Override
protected void onDestroy() {
super.onDestroy();
SMSSDK.unregisterAllEventHandler();
}</span>

4.初始化完了,我们就要请求验证码咯,为了达到平常那种验证码的效果 这里我写了个计时器 然后调用

<span style="font-size:18px;"> //计时器
private CountDownTimer timer = new CountDownTimer(60000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
btnView.setText((millisUntilFinished / 1000) + "秒后可重发");
}

@Override
public void onFinish() {
btnView.setEnabled(true);
btnView.setText("获取验证码");
}
};</span>
调用时:

<span style="font-size:18px;">  case R.id.get_btn:
btnView.requestFocus();
if (<span style="color:#ff0000;">vaildateinfo()</span>) {
//启动获取验证码 86是中国
String zh=csed1.getText().toString().trim();
<span style="color:#ff0000;"> SMSSDK.getVerificationCode("86", zh)</span>;
timer.start();
}
break;</span>
其中还有手机号和密码的验证 其中叶子定义了待删除图片的EditText

<span style="font-size:18px;"> //验证注册信息
private boolean <span style="color:#ff0000;">vaildateinfo()</span> {
csed1 = (EditText) findViewById(R.id.csed1);
csed2 = (EditText) findViewById(R.id.csed2);
String zh = csed1.getText().toString().trim();
String pwd = csed2.getText().toString().trim();
//首先要判断是否为空
if (!zh.equals("") || null != zh) {
if (zh.length() == 11) {
if (!pwd.equals("") || null != pwd) {
if (pwd.length() == 8) {
return true;
} else {
Toast.makeText(Regeister.this, "密码不足8位", Toast.LENGTH_SHORT).show();
csed2.requestFocus();
}
} else {
Toast.makeText(Regeister.this, "密码不能为空", Toast.LENGTH_SHORT).show();
csed2.requestFocus();
}
} else {
Toast.makeText(Regeister.this, "手机号不足11位", Toast.LENGTH_SHORT).show();
csed1.requestFocus();
}
} else {
Toast.makeText(Regeister.this, "手机号不能为空", Toast.LENGTH_SHORT).show();
csed1.requestFocus();
}
return false;
}</span>
带删除按钮的EditText,这里就不介绍了 之前写过,看前面写的博客

5.获取验证码后,验证验证码。我们要把得到的验证码填到编辑域吧(当然,手动填写是不是感觉已经out了 我们也可以自定义广播,拦截这样的信息 自动填入文本框中 这种效果很友好,后期我会讲到,我们先用老方法)

<span style="font-size:18px;">    //验证 验证码
private void vaildatePassword() {
String code = vaildatepwd.getText().toString().trim();
String zh=csed1.getText().toString().trim();
SMSSDK.submitVerificationCode("86", zh, code);
putUserInfo("86", zh);
}</span>

XML文件代码如下:

<span style="font-size:18px;"><?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:gravity="center"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/zh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号:"
android:textColor="#000000"
android:textSize="16sp" />

<com.example.boom.messageproject.ui.CustomEditText
android:id="@+id/csed1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="请输入您的手机号"
android:inputType="number"
android:maxLength="11"
android:paddingLeft="20sp" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">

<TextView
android:id="@+id/pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textColor="#000000"
android:textSize="16sp" />

<com.example.boom.messageproject.ui.CustomEditText
android:id="@+id/csed2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="请输入您的密码"
android:inputType="textPassword"
android:maxLength="8"
android:paddingLeft="20sp" />
</LinearLayout>

<LinearLayout
android:id="@+id/get_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">

<EditText
android:id="@+id/vaildatepwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="@null" />

<TextView
android:id="@+id/get_btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="2"
android:clickable="true"
android:gravity="center"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="获取验证码" />
</LinearLayout>


<Button
android:id="@+id/rgs_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="提交信息"
android:textColor="#000000"
android:textSize="16sp" />
</LinearLayout>

</LinearLayout></span>
java代码:

<span style="font-size:18px;">package com.example.boom.messageproject.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.boom.messageproject.R;

import java.util.Random;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

/**
* Created by Boom on 2016/8/2.
*/
public class Regeister extends Activity implements View.OnClickListener {
private static final String[] AVATARS = new String[400];
TextView btnView;
EditText csed1 = null, csed2 = null;
EditText vaildatepwd = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.regeister);
//注册短信回调
SMSSDK.registerEventHandler(new EventHandler() {
@Override
public void afterEvent(int event, int result, Object data) {
Message msg = Message.obtain();
msg.arg1 = event;
msg.arg2 = result;
msg.obj = data;
handler.sendMessage(msg);
}
});
findViewId();
init();
}

private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int event = msg.arg1;
int result = msg.arg2;
Object data = msg.obj;
if (result == SMSSDK.RESULT_COMPLETE) {
//回调完成
Toast.makeText(Regeister.this, "回调完成", Toast.LENGTH_SHORT).show();
if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
//提交验证码成功
Toast.makeText(Regeister.this, "提交验证码成功", Toast.LENGTH_SHORT).show();
} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
//获取验证码成功
Toast.makeText(Regeister.this, "获取验证码成功", Toast.LENGTH_SHORT).show();
} else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
//返回支持发送验证码的国家列表
Toast.makeText(Regeister.this, "返回支持发送验证码的国家列表", Toast.LENGTH_SHORT).show();
}
} else {
//回调失败
((Throwable) data).printStackTrace();
}
}
};

private void findViewId() {
btnView = (TextView) findViewById(R.id.get_btn);
vaildatepwd = (EditText) findViewById(R.id.vaildatepwd);

}

private void init() {
btnView.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
//点击获取验证码控件
case R.id.get_btn:
btnView.requestFocus();
if (vaildateinfo()) {
//启动获取验证码 86是中国
String zh = csed1.getText().toString().trim();
SMSSDK.getVerificationCode("86", zh);
timer.start();
}
break;
//点击提交信息按钮
case R.id.rgs_btn:
VaildateputInfo();
break;

}
}

/**
* 1.验证验证码
* 2.提交用户信息
*/
private void VaildateputInfo() {
vaildatePassword();
}

//验证 验证码
private void vaildatePassword() {
String code = vaildatepwd.getText().toString().trim();
String zh = csed1.getText().toString().trim();
SMSSDK.submitVerificationCode("86", zh, code);
putUserInfo("86", zh);
}

//提交用户信息
private void putUserInfo(String country, String phone) {
Random rnd = new Random();
int id = Math.abs(rnd.nextInt());
String uid = String.valueOf(id);
String nickName = "SmsSDK_User_" + uid;
String avatar = AVATARS[id % 12];
SMSSDK.submitUserInfo(uid, nickName, avatar, country, phone);
}

//计时器
private CountDownTimer timer = new CountDownTimer(60000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
btnView.setText((millisUntilFinished / 1000) + "秒后可重发");
}

@Override
public void onFinish() {
btnView.setEnabled(true);
btnView.setText("获取验证码");
}
};

@Override
protected void onDestroy() {
super.onDestroy();
SMSSDK.unregisterAllEventHandler();
}

//验证注册信息
private boolean vaildateinfo() {
csed1 = (EditText) findViewById(R.id.csed1);
csed2 = (EditText) findViewById(R.id.csed2);
String zh = csed1.getText().toString().trim();
String pwd = csed2.getText().toString().trim();
//首先要判断是否为空
if (!zh.equals("") || null != zh) {
if (zh.length() == 11) {
if (!pwd.equals("") || null != pwd) {
if (pwd.length() == 8) {
return true;
} else {
Toast.makeText(Regeister.this, "密码不足8位", Toast.LENGTH_SHORT).show();
csed2.requestFocus();
}
} else {
Toast.makeText(Regeister.this, "密码不能为空", Toast.LENGTH_SHORT).show();
csed2.requestFocus();
}
} else {
Toast.makeText(Regeister.this, "手机号不足11位", Toast.LENGTH_SHORT).show();
csed1.requestFocus();
}
} else {
Toast.makeText(Regeister.this, "手机号不能为空", Toast.LENGTH_SHORT).show();
csed1.requestFocus();
}
return false;
}

}
</span>
效果如下










更多相关文章

  1. Android调用百度地图Web端接口,实现百度定位、导航
  2. 避免在Java接口中使用数组的3个理由
  3. 【Java笔记】——抽象类和接口
  4. C#/Java 调用WSDL接口及方法
  5. 第三部分:Android 应用程序接口指南---第二节:UI---第六章 对话框
  6. sc7731 Android 5.1 Camera 学习之二 framework 到 HAL接口整理
  7. [Unity3D]调用Android接口
  8. Android添加一个回调监听接口
  9. Android下usb host接口插入usb设备时,如何屏蔽usb权限的提示框? 如

随机推荐

  1. android获取mac地址
  2. android自定义view属性
  3. Maven + Eclipse + Android 环境搭建
  4. android系统自带的Service原理与使用
  5. 设置Android状态栏的颜色
  6. android 配置属性
  7. Android--Selector、shape详解 (
  8. Android控制水平方向字体缩放android:tex
  9. Android表情功能
  10. Android 利用TCP通信 实现环境数据显示及