第三方集成之Mob-SMSSDk-短信验证

博客是给自己看的,所以看不懂很正常

 

第一步导jar包,配置androidmanifest文件,build.gradle配置

如果要调用api提供的验证界面,需要在Androidmainfest.xml文件注册
<activity
            android:name="com.mob.tools.MobUIShell"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:windowSoftInputMode="stateHidden|adjustResize"/>
、、、、、、、、、、、、、、、、、、、、

在libs文件夹下添加jar包之后,还需要在build.gradle文件下添加依赖
compile name: 'SMSSDK-3.0.0', ext: 'aar'
compile name: 'SMSSDKGUI-3.0.0', ext: 'aar'

 

第二步创建APP继承Application

public class app extends Application {
@Override
public void onCreate() {
super.onCreate();
// 通过代码注册你的AppKeyAppSecret
MobSDK.init(this, "209f7f6469ebc", "9de63027bbc5d5ea380558ae29c5c26b");
}
}

上代码吧不说了

package com.example.registerdemotwo;

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

import java.util.HashMap;

import cn.smssdk.EventHandler;
import cn.smssdk.OnSendMessageHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;

import static android.R.attr.country;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private EditText et_phone_number;
private EditText et_verification_code;

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

TextView tv_show_verification_pager = (TextView) findViewById(R.id.tv_show_verification_pager);
TextView tv_get_verification_code = (TextView) findViewById(R.id.tv_get_verification_code);
TextView tv_verification_code = (TextView) findViewById(R.id.tv_verification_code);
tv_show_verification_pager.setOnClickListener(this);
tv_get_verification_code.setOnClickListener(this);
tv_verification_code.setOnClickListener(this);

//输入框,手机号和验证码
et_phone_number = (EditText) findViewById(R.id.et_phone_number);
et_verification_code = (EditText) findViewById(R.id.et_verification_code);

//注册短信回调
SMSSDK.registerEventHandler(eh);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_show_verification_pager:
//打开注册页面
RegisterPage registerPage = new RegisterPage();
registerPage.setRegisterCallback(new EventHandler() {
public void afterEvent(int event, int result, Object data) {
// 解析注册结果
if (result == SMSSDK.RESULT_COMPLETE) {
@SuppressWarnings("unchecked")
HashMap<String, Object> phoneMap = (HashMap<String, Object>) data;
String country = (String) phoneMap.get("country");
String phone = (String) phoneMap.get("phone");
}
}
});
registerPage.show(MainActivity.this);
break;

//点击获取验证码
case R.id.tv_get_verification_code:
SMSSDK.getVerificationCode("86", et_phone_number.getText().toString().trim(), new OnSendMessageHandler() {
@Override
public boolean onSendMessage(String s, String s1) {
return false;
}
});

break;

//点击验证验证码是否正确
//手机号 加 验证码
case R.id.tv_verification_code:
SMSSDK.submitVerificationCode("86", et_phone_number.getText().toString().trim(), et_verification_code.getText().toString().trim());
break;
}
}

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) {
//提交验证码正确的回调
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "验证成功", Toast.LENGTH_SHORT).show();
}
});
} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
//获取验证码成功
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "获取验证码成功", Toast.LENGTH_SHORT).show();
}
});
} else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
//返回支持发送验证码的国家列表
}
} else {
((Throwable) data).printStackTrace();
Log.e("tag", ((Throwable) data).getMessage().toString());
//获取验证码成功
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "失败", Toast.LENGTH_SHORT).show();

}
});
}

}
};

@Override
protected void onDestroy() {
super.onDestroy();
SMSSDK.unregisterEventHandler(eh);
}
}

 

posted @ 2017-09-15 10:30  PowerSen  阅读(1630)  评论(0编辑  收藏  举报