sharedSDK短信验证码自制GUI

短信验证码无GUI:
    SDK下载以及APPID的获取参照 短信验证码有GUI教程
    同时关联SMSSDK即可

    jar包的拷贝
     尽管短信SDK提供了简化集成步骤的GUI功能,但它同样允许开发者放弃这套GUI,直接与核心代码进行交互。
    Studio: MobCommons-xxx.jar,MobTools-xxx.jar,SMSSDK-<版本号>.aar
    Eclipse:短信SDK的核心代码存放于 SMSSDK/libs中的“SMSSDK-<版本号>.jar”,集成的时候除了将SMSSDK-<版本号>.jar复制到自己的工程libs下,还要     复制MobCommons-xxx.jar,MobTools-xxx.jar和资源文件,还有SMSSDK的res下的raw要复制到你的工程的res下;如果版本小于2.0.1,不用复制raw,另       外要复制libsmssdk.so。
    其中对外主要的类有:SMSSDK和EventHandler,所有的操 作都通过SMSSDK来发起,并通过EventHandler来接收。

    XML代码:
<LinearLayout 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"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00ccff" >

        <ImageView
            android:id="@+id/iv_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:padding="10dp"
            android:src="@drawable/back" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="注册"
            android:textColor="@android:color/white"
            android:textSize="25sp"
            android:textStyle="bold" />
    </RelativeLayout>

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="昵 称:"
            android:textSize="20sp"
            android:textColor="#666699" />

        <EditText
            android:id="@+id/et_register_nick"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="3dp"
            android:hint="输入昵称" />
    </LinearLayout>

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="6dp"
            android:text="性  别:"
            android:textColor="#666699"
            android:textSize="20sp" />

        <CheckBox
            android:id="@+id/cb_sex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/checkbox_selector"
            android:button="@null"
            android:checked="true" />
    </LinearLayout>

    <EditText
        android:id="@+id/et_register_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:hint="输入手机号"
        android:padding="6dp" />

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

        <EditText
            android:id="@+id/et_code"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_weight="2"
            android:hint="输入验证码"
            android:padding="6dp" />

        <Button
            android:id="@+id/btn_getCode"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#00ccff"
            android:padding="6dp"
            android:text="获取验证码"
            android:textColor="#fff" >
        </Button>
    </LinearLayout>

    <EditText
        android:id="@+id/et_register_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:hint="输入密码"
        android:padding="6dp"
        android:password="true" />

    <Button
        android:id="@+id/btn_register"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/button_selector"
        android:padding="10dp"
        android:text="注册"
        android:textColor="#fff"
        android:textSize="16sp"
        android:textStyle="bold" />

</LinearLayout>

    activity实现代码:
package com.wang.smsdemo2;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

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


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.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener, Callback{
private ImageView iv_back;
private EditText et_register_nick;
private EditText et_register_password,et_number,et_code;
private Button btn_getCode,btn_register;
//private RegisterData data;
private static int time=60;

String APPKEY="10a869a5f93c6";
String APPSECRETEString="c2f73b5c6cc64fe653cae7a29ec37de5";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);

initSMS();
initView();
}

private void initSMS() {
try{
//初始化SMSSDK
SMSSDK.initSDK(this, APPKEY, APPSECRETEString);
final Handler handler = new Handler(this);
EventHandler eventHandler=new EventHandler(){
@Override
public void afterEvent(int event, int result, Object data) {
super.afterEvent(event, result, 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
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) {
System.out.println("--------result"+event);
//回调完成
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{                                                                
((Throwable)data).printStackTrace();
Toast.makeText(MainActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();
}
return false;
}
public void initView() {

et_register_nick=(EditText)findViewById(R.id.et_register_nick);
et_register_password=(EditText)findViewById(R.id.et_register_password);
et_number=(EditText)findViewById(R.id.et_register_number);
et_code=(EditText)findViewById(R.id.et_code);
btn_getCode=(Button)findViewById(R.id.btn_getCode);
btn_register=(Button)findViewById(R.id.btn_register);
iv_back=(ImageView)findViewById(R.id.iv_back);

iv_back.setOnClickListener(this);
btn_getCode.setOnClickListener(this);
btn_register.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_back:
finish();
break;
case R.id.btn_getCode:
getCode();
break;

case R.id.btn_register:
Register();
break;
default:
break;
}
}
private void getCode() {
if(isMobileNO(et_number.getText().toString())){
//申请验证码,EventHandler回调
SMSSDK.getVerificationCode("86", et_number.getText().toString());
if(time>0){
handler.postDelayed(runnable, 1000);
}else{
time=60;
handler.postDelayed(runnable, 1000);
}
}else{
Toast.makeText(MainActivity.this, "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
}

}
private void Register() {
String id=et_number.getText().toString().trim();
String pw=et_register_password.getText().toString().trim();
String nick=et_register_nick.getText().toString().trim();
String code=et_code.getText().toString().trim();
if(TextUtils.isEmpty(id)||TextUtils.isEmpty(pw)||TextUtils.isEmpty(nick)||TextUtils.isEmpty(code)){
Toast.makeText(MainActivity.this, "请将资料填写完整", Toast.LENGTH_SHORT).show();
return;
}
if(pw.length()<6){
Toast.makeText(MainActivity.this, "密码少于6位", Toast.LENGTH_SHORT).show();
return;
}

//提交验证码,EventHandler回调
SMSSDK.submitVerificationCode("86", id, code);

//getData(id,pw);
}
//获取验证码按钮60s不能点击
Handler handler=new Handler();
Runnable runnable=new Runnable() {

@Override
public void run() {
time--;
if(time>0){
btn_getCode.setText(time+"s后重发");
btn_getCode.setBackgroundResource(R.drawable.btn_confirm_out);
btn_getCode.setEnabled(false);
handler.postDelayed(this, 1000);
}else{
btn_getCode.setText("获取验证码");
btn_getCode.setBackgroundResource(R.drawable.btn_confirm_on);
btn_getCode.setEnabled(true);
}

}
};
// 验证电话号码正则表达式
public boolean isMobileNO(String mobiles) {
Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[^1^4,\\D]))\\d{8}");
Matcher m = p.matcher(mobiles);
return m.matches();
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值