短信验证

登陆mob   SMSAndroid集成文档按步骤进行
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText phone;
    private EditText yanzhengma;
    private TextView huoqu;
    private TextView huodeyanzheng1;
    private TextView huodeyanzheng2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        phone = (EditText) findViewById(R.id.phone);
        yanzhengma = (EditText) findViewById(R.id.yanzhengma);
        huoqu = (TextView) findViewById(R.id.huoqu);
        huodeyanzheng1 = (TextView) findViewById(R.id.huodeyanzheng1);
        huodeyanzheng2 = (TextView) findViewById(R.id.huodeyanzheng2);
        huoqu.setOnClickListener(this);
        huodeyanzheng1.setOnClickListener(this);
        huodeyanzheng2.setOnClickListener(this);
       SMSSDK.registerEventHandler(eh);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.huoqu:
               SMSSDK.getVerificationCode("86", phone.getText().toString().trim(), new OnSendMessageHandler() {
                   @Override
                   public boolean onSendMessage(String s, String s1) {
                       return false;
                   }
               });
                break;
            case R.id.huodeyanzheng1:
                SMSSDK.submitVerificationCode("86", phone.getText().toString().trim(), yanzhengma.getText().toString().trim());

                break;
            case R.id.huodeyanzheng2:
                //打开注册页面
                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;
        }
    }
    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,"huoqu成功",Toast.LENGTH_SHORT).show();
                        }
                    });
                    //提交验证码成功
                }else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this,"huoqueyanzhengma1成功",Toast.LENGTH_SHORT).show();
                        }
                    });
                    //获取验证码成功
                }else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){
                    //返回支持发送验证码的国家列表
                }
            }else{
                ((Throwable)data).printStackTrace();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this,"失败",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
    };
    @Override
    protected void onDestroy() {
        super.onDestroy();
        SMSSDK.registerEventHandler(eh); //注册短信回调
    }
}
public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        MobSDK.init(this, "1ff9f9bbc1510", "f06175272b7151b0eca792ecbb0e3be0");
    }
}
XML:
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.fanyishuo.yanzhengma.MainActivity">
<EditText
    android:id="@+id/phone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入电话号"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="15dp"
        >
        <EditText
            android:id="@+id/yanzhengma"
            android:layout_width="182dp"
            android:layout_height="wrap_content"
            android:hint="请输入验证码"/>
        <TextView
            android:id="@+id/huoqu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="获取验证码"
            android:textSize="25sp"
            />
    </LinearLayout>
    <TextView
        android:id="@+id/huodeyanzheng1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="验证"
        android:textSize="25sp"
        android:layout_marginTop="15dp"
        />
    <TextView
        android:id="@+id/huodeyanzheng2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="短信验证"
        android:textSize="25sp"
        android:layout_marginTop="15dp"
        android:layout_gravity="center"
        />
</LinearLayout>


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值