Android 短信验证
来源:互联网 发布:黑莓playbook解网络锁 编辑:程序博客网 时间:2024/05/15 05:07
Mob官网:http://www.mob.com/#/
SDK–>SMS For Android
官方教程:
http://wiki.mob.com/android-%E7%9F%AD%E4%BF%A1sdk%E9%9B%86%E6%88%90%E6%96%87%E6%A1%A3/
Java代码:
package com.example.x.sms_text;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.EditText;import org.json.JSONException;import org.json.JSONObject;import cn.smssdk.EventHandler;import cn.smssdk.SMSSDK;public class MainActivity extends Activity implements View.OnClickListener { EditText editText2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btn1).setOnClickListener(this); findViewById(R.id.btn2).setOnClickListener(this); SMSSDK.initSDK(this, App Key, App Secret);//官网申请 EventHandler eh=new EventHandler(){ @Override public void afterEvent(int event, int result, Object data) { if (result == SMSSDK.RESULT_COMPLETE) { //回调完成 if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) { //提交验证码成功 //验证码正确,执行后续操作 System.out.println("提交成功"); }else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){ //获取验证码成功 System.out.println("获取成功"); }else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){ //返回支持发送验证码的国家列表 System.out.println("国家列表"); } }else if (result == SMSSDK.RESULT_ERROR) { //验证码错误 System.out.println("验证失败"); Throwable throwable = (Throwable) data; throwable.printStackTrace(); JSONObject object; try { object = new JSONObject(throwable.getMessage()); System.out.println(object.optInt("status"));//错误代码 System.out.println(object.optString("detail"));//错误描述 } catch (JSONException e) { e.printStackTrace(); } } else{ //其他错误 ((Throwable)data).printStackTrace(); } } }; SMSSDK.registerEventHandler(eh); //注册短信回调 } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn1: getMsg(); break; case R.id.btn2: sendMsg(editText2.getText().toString()); break; } } public static void getMsg() { //获取验证码 SMSSDK.getVerificationCode("86","18000000000"); } public static void sendMsg(String code){ //提交验证码 SMSSDK.submitVerificationCode("86","18000000000",code); }}
build.gradle(Module:app)添加如下代码:
repositories{ flatDir{ dirs 'libs' //就是你放aar的目录地址 }}
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.4.0' compile files('libs/MobCommons-2016.0714.1402.jar') compile files('libs/MobTools-2016.0714.1402.jar') compile name:'SMSSDK-2.1.1',ext:'aar'//添加 compile name:'SMSSDKGUI-2.1.1',ext:'aar'//添加}
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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.x.sms_text.MainActivity"> <EditText android:id="@+id/et2" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" android:hint="bb"/> <Button android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" android:text="submit1" /> <Button android:id="@+id/btn2" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" android:text="submit2" /></LinearLayout>
权限:
<uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.READ_SMS" /> <uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
0 0
- android短信验证功能
- Android 短信验证
- android之短信验证
- Android-短信验证功能
- Android短信验证
- Android短信验证+源码
- Android短信验证
- Android 短信验证
- Android 短信验证
- Android短信验证码
- Android SMSSDK短信验证
- Android短信验证功能
- Android短信验证码
- android 短信验证倒计时功能
- Android 短信验证码控件
- android 获取短信验证码
- Android之短信验证码
- Android之短信验证码
- libevent学习笔记 ---- 回显服务器 (2)
- SVN版本控制与分支设置
- Xcode6之后添加PCH文件
- C++ typedef的用法
- 在Block中使用weakSelf与strongSelf的意义
- Android 短信验证
- cusen系列鞋
- 申请硕士 澳门大学一位老师的忠告
- 稀疏图~稠密图
- Android UI-开源框架ImageLoader的完美例子
- 失业保险金的领取及数据库实现
- Android ClearEditText:输入用户名、密码错误时整体删除及输入为空时候晃动提示
- If everybody is thinking alike
- Laravel API笔记