iwanghang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

build.gradle(Project:MyApp)

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    
    repositories {
        maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
        google()
        jcenter()
        // 添加MobSDK的maven地址
        maven {
            url "http://mvn.mob.com/android"
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        // 注册MobSDK
        classpath "com.mob.sdk:MobSDK:+"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle(Module:app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.gsw.emg895"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:design:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    // 微信SDK
    // compile 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'
}

// MOB 添加插件
apply plugin: 'com.mob.sdk'

// 在MobSDK的扩展中注册SMSSDK的相关信息
MobSDK {
    appKey "000000000000000"
    appSecret "0000000000000000000000000"

    // MOB短信验证
    SMSSDK {}
   
}
MainActivity
package com.gsw.emg895;

import android.os.Bundle;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.mob.MobSDK;

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

public class MainActivity3 extends AppCompatActivity {

    private EditText et_phone;
    private EditText et_phone_code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MobSDK.init(this);
        initView();

    }

    /**
     * 初始化UI
     */
    private void initView() {
        et_phone = findViewById(R.id.et_phone);
        et_phone_code = findViewById(R.id.et_phone_code);
    }

    private void myToast(final String s) {
        new Thread() {
            public void run() {
                Looper.prepare();
                Toast.makeText(MainActivity3.this, "" + s, Toast.LENGTH_LONG).show();
                Looper.loop();
            }
        }.start();
    }

    /**
     * 发送验证码
     */
    public void send_code(View view){
        String phone = et_phone.getText().toString();
        sendCode("86",phone);
    }

    /**
     * 验证验证码
     */
    public void check_code(View view) {
        String phone = et_phone.getText().toString();
        String phone_code = et_phone_code.getText().toString();
        submitCode("86", phone, phone_code);
    }



    // 请求验证码,其中country表示国家代码,如“86”;phone表示手机号码,如“13800138000”
    public void sendCode(String country, String phone) {
        // 注册一个事件回调,用于处理发送验证码操作的结果
        SMSSDK.registerEventHandler(new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE) {
                    // TODO 处理成功得到验证码的结果
                    // 请注意,此时只是完成了发送验证码的请求,验证码短信还需要几秒钟之后才送达
                    Log.i("yzyz","send ok");
                    myToast("验证码已发送");
                } else{
                    // TODO 处理错误的结果
                    Log.i("yzyz","send error");
                    myToast("验证码发送失败,请稍后再试");
                }
                //用完回调要注销掉,否则可能会出现内存泄露
                SMSSDK.unregisterAllEventHandler();
            }
        });
        // 触发操作
        SMSSDK.getVerificationCode(country, phone);
    }

    // 提交验证码,其中的code表示验证码,如“1357”
    public void submitCode(String country, String phone, String code) {
        // 注册一个事件回调,用于处理提交验证码操作的结果
        SMSSDK.registerEventHandler(new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE) {
                    // TODO 处理验证成功的结果
                    Log.i("yzyz","yes");
                    myToast("验证码验证成功");
                } else{
                    // TODO 处理错误的结果
                    Log.i("yzyz","no");
                    myToast("验证码验证失败");
                }
                //用完回调要注销掉,否则可能会出现内存泄露
                SMSSDK.unregisterAllEventHandler();
            }
        });
        // 触发操作
        SMSSDK.submitVerificationCode(country, phone, code);
    }

    protected void onDestroy() {
        super.onDestroy();
        //用完回调要注销掉,否则可能会出现内存泄露
        SMSSDK.unregisterAllEventHandler();
    }


}

activity_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"
    android:orientation="vertical"
    tools:context="com.gsw.emg895.MainActivity3">

    <EditText
        android:id="@+id/et_phone"
        android:hint="手机号码"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:onClick="send_code"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送验证码"/>

    <EditText
        android:id="@+id/et_phone_code"
        android:hint="验证码"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="check_code"
        android:text="验证验证码"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="48dp" />

</LinearLayout>

集成文档:

 http://wiki.mob.com/sdk-sms-android-3-0-0/

 

posted on 2018-03-29 11:38  iwanghang  阅读(526)  评论(0编辑  收藏  举报