短信验证码的简单实现
今天做了一个简单的短信验证小项目,来给大家分享一下
集成环境可用参照这篇文档 http://wiki.mob.com/sdk-sms-android-3-0-0/ 这是官网的文档很详细,集成到
二 、添加代码
就可以了,废话不多说直接看代码
布局文件代码
<LinearLayout 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" android:orientation="vertical" tools:context="com.example.smsdemo.MainActivity"> <EditText android:id="@+id/phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入手机号"/> <Button android:id="@+id/btn_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="获取验证码" android:onClick="test"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="验证码:"/> <EditText android:id="@+id/code" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="验证码"/> <Button android:id="@+id/btn_code" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="开始验证"/> </LinearLayout>
找到相应的控件并设置监听
phone = (EditText) findViewById(R.id.phone); code = (EditText) findViewById(R.id.code); btn_phone = (Button) findViewById(R.id.btn_phone); btn_code = (Button) findViewById(R.id.btn_code); btn_phone.setOnClickListener(this); btn_code.setOnClickListener(this);
点击事件相应的代码
@Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_phone: //获取验证码 String phonenumber = phone.getText().toString().trim(); if (!phonenumber.equals("")) { if (checkTel(phonenumber)) { SMSSDK.getVerificationCode("+86", phonenumber);//发短信获取验证码 Log.e("666","开始发短信"); code.requestFocus(); mTimeCount.start(); } else { Toast.makeText(getApplicationContext(), "请正确输入手机号", Toast.LENGTH_SHORT).show(); phone.requestFocus(); } } else { Toast.makeText(getApplicationContext(), "请输入手机号", Toast.LENGTH_SHORT).show(); phone.requestFocus(); } break; case R.id.btn_code: //验证 //String code = this.code.getText().toString().trim(); if (!phone.getText().toString().trim().equals("")) { if (checkTel(phone.getText().toString().trim())) { if (!code.getText().toString().trim().equals("")) { //验证码已经输入 进行验证 SMSSDK.submitVerificationCode("+86", phone.getText().toString().trim(), code.getText().toString().trim()); flag = false; } else { Toast.makeText(getApplicationContext(), "请正确验证码", Toast.LENGTH_SHORT).show(); code.requestFocus(); } } else { Toast.makeText(getApplicationContext(), "请正确输入手机号", Toast.LENGTH_SHORT).show(); phone.requestFocus(); } } else { Toast.makeText(getApplicationContext(), "请输入手机号", Toast.LENGTH_SHORT).show(); phone.requestFocus(); } break; } } 事件回调
private void init() { 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) {//提交验证码成功 startActivity(new Intent(MainActivity.this, Login.class)); } 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 { runOnUiThread(new Runnable() { @Override public void run() { if(flag){ Toast.makeText(MainActivity.this,"验证码获取失败",Toast.LENGTH_SHORT).show(); }else{ // ((Throwable) data).printStackTrace(); Toast.makeText(MainActivity.this,"验证码错误",Toast.LENGTH_SHORT).show(); } } }); } } }; SMSSDK.registerEventHandler(eh); } Toast 如何不在UI线程中进行会报错 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()下载地址:https://download.****.net/my