Mob手机验证码android studio实现
2017-10-23 19:24
162 查看
最近自己想做个APP玩玩,想加上手机注册功能,经过多次网上查询,决定使用mob的免费验证码。在网上查询了很多关于mob的资料,好像没有关于最近一个版本的使用,自己探索了一段时间,感觉不懂的人会感觉很烦,决定写一个demo帮助一下有需要的同学。(包含SDK自带的GUI和自己设计的GUI)
界面如下
1.注册一个有效的mob账号
2.进入mob后台,使用短信验证SDK,然后添加一个应用(可以是空的应用,后面可以继续登记)
3.下载sdk,短信验证SDK,最近版本(
)
4.环境配置,解压缩后的SDK包中的jar包添加和AndroidManifest.xml文件的修改参照
-- 此 处
5.layout布局文件(acyivity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.demo9.MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="50dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="sdk自带的GUI"
android:textColor="@color/colorAccent"
android:gravity="center"
android:textSize="16dp"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="验证码"
android:layout_gravity="center"
android:layout_margin="20dp"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="@color/colorPrimary"/>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义UI"
android:textColor="@color/colorAccent"
android:gravity="center"
android:textSize="16dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:id="@+id/editText"
android:hint="填写手机号码"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="获取验证码"
android:id="@+id/button"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:id="@+id/editText2"
android:hint="填写验证码" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="发送验证码"
android:id="@+id/button2" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
6.MainActivity.java
4000
package com.example.demo9;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONObject;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;
import cn.smssdk.utils.SMSLog;
public class MainActivity extends AppCompatActivity {
private Button btn1,btn2,btn3;
private EditText edt1,edt2;
private String phone;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
btn1 = (Button)findViewById(R.id.button);
btn2 = (Button)findViewById(R.id.button2);
btn3 = (Button) findViewById(R.id.button3);
edt1 = (EditText)findViewById(R.id.editText);
edt2 = (EditText)findViewById(R.id.editText2);
EventHandler eh=new EventHandler(){
@Override
public void afterEvent(int event, int result, Object data) {
Message msg = new Message();
msg.arg1 = event;
msg.arg2 = result;
msg.obj = data;
mHandler.sendMessage(msg);
}
};
SMSSDK.registerEventHandler(eh);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//请求获取短信验证码
SMSSDK.getVerificationCode("86", edt1.getText().toString());
phone = edt1.getText().toString();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//提交短信验证码
SMSSDK.submitVerificationCode("86",phone,edt2.getText().toString());
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
opeRegisterPager();
}
});
}
Handler mHandler = new Handler()
{
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
int event = msg.arg1;
int result = msg.arg2;
Object data = msg.obj;
Log.e("event", "event=" + event);
if (result == SMSSDK.RESULT_COMPLETE) {
System.out.println("--------result"+event);
//短信注册成功后,返回MainActivity,然后提示新好友
if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
//提交验证码成功
Toast.makeText(getApplicationContext(), "提交验证码成功", Toast.LENGTH_SHORT).show();
} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
//已经验证
Toast.makeText(getApplicationContext(), "验证码已经发送", Toast.LENGTH_SHORT).show();
}
} else {
int status = 0;
try {
((Throwable) data).printStackTrace();
Throwable throwable = (Throwable) data;
JSONObject object = new JSONObject(throwable.getMessage());
String des = object.optString("detail");
status = object.optInt("status");
if (!TextUtils.isEmpty(des)) {
Toast.makeText(MainActivity.this, des, Toast.LENGTH_SHORT).show();
return;
}
} catch (Exception e) {
SMSLog.getInstance().w(e);
}
}
}
};
protected void onDestroy() {
super.onDestroy();
SMSSDK.unregisterAllEventHandler();
};
private void opeRegisterPager() {
//打开注册页面,这个页面就是我们在AndroidManifest.xml中添加的节点com.mob.tools.MobUIShell
RegisterPage registerPage = new RegisterPage();
registerPage.setRegisterCallback(new EventHandler() {
public void afterEvent(int event, int result, Object data) {
// 解析注册结果
if (result == SMSSDK.RESULT_COMPLETE) {
}
}
});
registerPage.show(context);
}
}
界面如下
1.注册一个有效的mob账号
2.进入mob后台,使用短信验证SDK,然后添加一个应用(可以是空的应用,后面可以继续登记)
3.下载sdk,短信验证SDK,最近版本(
SMSSDK For Android v3.0.0 (2017-06-12更新)
)4.环境配置,解压缩后的SDK包中的jar包添加和AndroidManifest.xml文件的修改参照
-- 此 处
5.layout布局文件(acyivity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.demo9.MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="50dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="sdk自带的GUI"
android:textColor="@color/colorAccent"
android:gravity="center"
android:textSize="16dp"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="验证码"
android:layout_gravity="center"
android:layout_margin="20dp"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="@color/colorPrimary"/>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义UI"
android:textColor="@color/colorAccent"
android:gravity="center"
android:textSize="16dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:id="@+id/editText"
android:hint="填写手机号码"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="获取验证码"
android:id="@+id/button"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:id="@+id/editText2"
android:hint="填写验证码" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="发送验证码"
android:id="@+id/button2" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
6.MainActivity.java
4000
package com.example.demo9;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONObject;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;
import cn.smssdk.utils.SMSLog;
public class MainActivity extends AppCompatActivity {
private Button btn1,btn2,btn3;
private EditText edt1,edt2;
private String phone;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
btn1 = (Button)findViewById(R.id.button);
btn2 = (Button)findViewById(R.id.button2);
btn3 = (Button) findViewById(R.id.button3);
edt1 = (EditText)findViewById(R.id.editText);
edt2 = (EditText)findViewById(R.id.editText2);
EventHandler eh=new EventHandler(){
@Override
public void afterEvent(int event, int result, Object data) {
Message msg = new Message();
msg.arg1 = event;
msg.arg2 = result;
msg.obj = data;
mHandler.sendMessage(msg);
}
};
SMSSDK.registerEventHandler(eh);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//请求获取短信验证码
SMSSDK.getVerificationCode("86", edt1.getText().toString());
phone = edt1.getText().toString();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//提交短信验证码
SMSSDK.submitVerificationCode("86",phone,edt2.getText().toString());
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
opeRegisterPager();
}
});
}
Handler mHandler = new Handler()
{
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
int event = msg.arg1;
int result = msg.arg2;
Object data = msg.obj;
Log.e("event", "event=" + event);
if (result == SMSSDK.RESULT_COMPLETE) {
System.out.println("--------result"+event);
//短信注册成功后,返回MainActivity,然后提示新好友
if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
//提交验证码成功
Toast.makeText(getApplicationContext(), "提交验证码成功", Toast.LENGTH_SHORT).show();
} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
//已经验证
Toast.makeText(getApplicationContext(), "验证码已经发送", Toast.LENGTH_SHORT).show();
}
} else {
int status = 0;
try {
((Throwable) data).printStackTrace();
Throwable throwable = (Throwable) data;
JSONObject object = new JSONObject(throwable.getMessage());
String des = object.optString("detail");
status = object.optInt("status");
if (!TextUtils.isEmpty(des)) {
Toast.makeText(MainActivity.this, des, Toast.LENGTH_SHORT).show();
return;
}
} catch (Exception e) {
SMSLog.getInstance().w(e);
}
}
}
};
protected void onDestroy() {
super.onDestroy();
SMSSDK.unregisterAllEventHandler();
};
private void opeRegisterPager() {
//打开注册页面,这个页面就是我们在AndroidManifest.xml中添加的节点com.mob.tools.MobUIShell
RegisterPage registerPage = new RegisterPage();
registerPage.setRegisterCallback(new EventHandler() {
public void afterEvent(int event, int result, Object data) {
// 解析注册结果
if (result == SMSSDK.RESULT_COMPLETE) {
}
}
});
registerPage.show(context);
}
}
相关文章推荐
- VB与Excel2000实现复杂报表打印(转载)
- 使用Javascript实现QQ面板
- ASP.NET多频道网站架构实现方法
- JavaScript实现类,有多种方法。
- 使用AF_INET实现点对点的通信示例
- 重写UIPageControl实现自定义按钮
- Trie树 c++实现
- Ext4+Servlet+HTML5实现实时获取文件上传进度、本地预览、文件拖拽上传的相册实例
- 两个栈实现一个队列以及两个队列实现一个栈(Java)
- 报表模板实现网格式自定义报表
- ASP.NET jQuery 随笔 使用jQuery UI的Autocomplete方法实现文本框的自动搜索填充功能
- 解决Extjs4.1+Spring实现web登录发生的问题
- Java实现 数据结构之邻接矩阵
- KMP算法实现
- 基于客户端轮询服务器思想实现的网页即时通信Demo
- php实现曲线图和柱状图
- TraceRoute(tracert)源码(基于原始套接字实现)
- 用开源项目ActivityOptionsICS让ActivityOptions的动画实现兼容
- javascript Table 中2个列(TD)的交换实现代码
- jsp实现简单验证码的方法