Mob手机验证码android studio实现
来源:互联网 发布:淘宝客服介入会怎么样 编辑:程序博客网 时间:2024/05/21 21:01
最近自己想做个APP玩玩,想加上手机注册功能,经过多次网上查询,决定使用mob的免费验证码。在网上查询了很多关于mob的资料,好像没有关于最近一个版本的使用,自己探索了一段时间,感觉不懂的人会感觉很烦,决定写一个demo帮助一下有需要的同学。(包含SDK自带的GUI和自己设计的GUI)
界面如下
- 1.注册一个有效的mob账号
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
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 0
- Mob手机验证码android studio实现
- Android--使用Mob实现获取免费短信验证码(Android Studio)
- Android studio 下使用mob的短信验证码SMSSDK
- Mob手机验证
- 使用mob实现短信验证码验证
- Android使用mob实现手机号验证注册
- 使用mob实现注册验证码
- Mob免费验证码androidStudio实现
- Android Studio Mob快速集成短信验证(图文教程)
- Android Studio Mob快速集成短信验证(图文教程)
- android mob 短信验证码 功能
- Android 免费短信验证码--Mob.com
- android短信验证码(mob)
- 集成Mob 3.0免费获取手机验证码
- Android studio "社会化分享"实现步骤(总结mob网步骤)
- android短信验证之Mob
- 【mob】Android短信验证+源码
- 基于mob平台,手机短信验证码实现登录功能
- angularJs 正 到 排序
- rest服务理解以及restful api
- 基于java的设计模式的代理模式
- thread相关——互斥锁
- 论文阅读:《Realtime Multi-Person 2D Pose Estimation using Part Affinity Fields》CVPR 2017
- Mob手机验证码android studio实现
- Linux Unit11--系统中的文件传输
- Construct2从入门到不放弃
- logback日志框架的知识
- 系统开发排行榜
- SQLite查询计划器和优化(译)
- 2017.10.23 卡牌游戏 失败总结
- ionic练习(6)- 用户管理(添加,查询,排序)
- java面向对象四大特性-多态