短信验证

来源:互联网 发布:营销推广案例 知乎 编辑:程序博客网 时间:2024/05/29 10:56
登陆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>


 
原创粉丝点击