SMSSDK无GUI集成

直接贴代码

package com.msg.lyxin.myapplication_smssdk;

import android.app.Activity;
import android.app.ProgressDialog;
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 java.util.HashMap;

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

public class MainActivity extends Activity {
    Button button;
    private EditText et1, et2;
    private String phoneNum;
    private static String APPKEY = "15cc34e918b46";
    private static String APPSECRET = "d7bc5860002819a41b07fed8470cd127";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et1 = (EditText) findViewById(R.id.number);
        et2 = (EditText) findViewById(R.id.key);
        button = (Button) findViewById(R.id.button);

        SMSSDK.initSDK(this, APPKEY, APPSECRET);
        SMSSDK.registerEventHandler(eh);
    }

    private EventHandler eh = new EventHandler() {
        @Override
        public void afterEvent(int event, int result, Object data) {//提交验证码成功,如果验证成功会在data里返回数据。data数据类型为HashMap<number,code>
            if (result == SMSSDK.RESULT_COMPLETE) {
                //回调完成
                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                    //提交验证码成功
                    HashMap<String, Object> mData = (HashMap<String, Object>) data;
                    String country = (String) mData.get("country");//返回的国家编号
                    String phone = (String) mData.get("phone");//返回用户注册的手机号

                    if (phone.equals(phoneNum)) {
                        runOnUiThread(new Runnable() {//更改ui的操作要放在主线程,实际可以发送hander
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "通过验证", Toast.LENGTH_SHORT).show();
                            }
                        });
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "验证失败", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                    //获取验证码成功
                } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                    //返回支持发送验证码的国家列表
                }
            } else {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, "验证失败", Toast.LENGTH_SHORT).show();
                    }
                });
                ((Throwable) data).printStackTrace();
            }
        }
    };


    public void getSecurity(View v) {
        phoneNum = et1.getText().toString().trim();
        //发送短信,传入国家号和电话---使用SMSSDK核心类之前一定要在MyApplication中初始化,否侧不能使用
        if (TextUtils.isEmpty(phoneNum)) {
            Toast.makeText(this, "号码不能为空!", Toast.LENGTH_SHORT).show();
        } else {
            SMSSDK.getVerificationCode("+86", phoneNum);
            Toast.makeText(this, "发送成功:" + phoneNum, Toast.LENGTH_SHORT).show();
        }
    }

    public void testSecurity(View v) {
        String security = et2.getText().toString();
        if (!TextUtils.isEmpty(security)) {
            //提交短信验证码
            SMSSDK.submitVerificationCode("+86", phoneNum, security);//国家号,手机号码,验证码
        } else {
            Toast.makeText(this, "验证码不能为空", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //要在activity销毁时反注册,否侧会造成内存泄漏问题
        SMSSDK.unregisterAllEventHandler();
    }
}

 

posted @ 2016-08-24 16:20  Lyxin_c  阅读(398)  评论(0编辑  收藏  举报