android ---------------- 手機驗證碼

 

//手機驗證
compile 'com.github.yidun:captcha-android-demo:2.4.2'
implementation 'com.android.support:recyclerview-v7:26.1.0'

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".view.activtiys.ForgetActivity">

    <EditText
        android:hint="請輸入手機號"
        android:layout_marginTop="210dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_alignParentStart="true"
        android:layout_toStartOf="@+id/button"/>

    <Button
        android:background="@drawable/shap"
        android:layout_marginTop="210dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="獲取驗證碼"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"/>



    <Button
        android:layout_marginTop="20dp"
        android:background="@drawable/shap"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="點擊驗證"
        android:id="@+id/button2"
        android:layout_below="@+id/button"
        android:layout_alignStart="@+id/button"/>

    <EditText
        android:layout_marginTop="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText2"
        android:layout_below="@+id/editText"
        android:layout_alignParentStart="true"
        android:layout_alignEnd="@+id/editText"/>
</RelativeLayout>

 

package com.example.why.myapplication.view.activtiys;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
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 com.example.why.myapplication.R;

import org.json.JSONObject;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.utils.SMSLog;

public class ForgetActivity extends AppCompatActivity {
    private Button btn1, btn2;
    private EditText edt1, edt2;
    private String phone;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget);
        btn1 = (Button) findViewById(R.id.button);
        btn2 = (Button) findViewById(R.id.button2);
        edt1 = (EditText) findViewById(R.id.editText);
        edt2 = (EditText) findViewById(R.id.editText2);
        EventHandler eh = new EventHandler() {
            @Override
            public void afterEvent(int event, int result, Object data) {
                Message msg = new Message();
                msg.arg1 = event;
                msg.arg2 = result;
                msg.obj = data;
                mHandler.sendMessage(msg);
            }

        };
        SMSSDK.registerEventHandler(eh);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(getApplicationContext(), "123", Toast.LENGTH_SHORT).show();
                //SMSSDK.getVerificationCode("1", "4432487596");
                SMSSDK.getVerificationCode("86", edt1.getText().toString());
                phone = edt1.getText().toString();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SMSSDK.submitVerificationCode("86", phone, edt2.getText().toString());
            }
        });
    }
    @SuppressLint("HandlerLeak")
    Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {

            // TODO Auto-generated method stub
            super.handleMessage(msg);
            int event = msg.arg1;
            int result = msg.arg2;
            Object data = msg.obj;
            Log.e("event", "event=" + event);
            if (result == SMSSDK.RESULT_COMPLETE) {
                System.out.println("--------result" + event);
                //短信註冊成功後,返回MainActivity,然後提示新好友
                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {//提交驗證碼成功
                    String s = edt1.getText().toString();
                    Toast.makeText(getApplicationContext(), "提交驗證碼成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(ForgetActivity.this, AgainActivity.class);
                    intent.putExtra("moblie",s);
                    startActivity(intent);
                    finish();

                } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                    //已經驗證
                    Toast.makeText(getApplicationContext(), "驗證碼已經發送", Toast.LENGTH_SHORT).show();
                } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                    //已經驗證
                    Toast.makeText(getApplicationContext(), "獲取國家列表成功", Toast.LENGTH_SHORT).show();
//                    textV.setText(data.toString());
                }

            } else {
//              ((Throwable) data).printStackTrace();
//              Toast.makeText(MainActivity.this, "驗證碼錯誤", Toast.LENGTH_SHORT).show();
//                  Toast.makeText(MainActivity.this, "123", Toast.LENGTH_SHORT).show();
                int status = 0;
                try {
                    ((Throwable) data).printStackTrace();
                    Throwable throwable = (Throwable) data;

                    JSONObject object = new JSONObject(throwable.getMessage());
                    String des = object.optString("detail");
                    status = object.optInt("status");
                    if (!TextUtils.isEmpty(des)) {
                        Toast.makeText(ForgetActivity.this, des, Toast.LENGTH_SHORT).show();
                        return;
                    }
                } catch (Exception e) {
                    SMSLog.getInstance().w(e);
                }
            }
        }
    };


    }

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章