个人应用,需要短信验证这么一个功能的话,mob的短信验证还是不错的。下面说一下集成过程
1.mob平台注册账号http://www.mob.com/
2.创建一个应用。对应会生成一个Appkey和 App Secret。
3.在新建项目左边栏目,将SMSSDK产品添加进去
4.下载SMDSDK,导入工程。(参照官方集成指南http://wiki.mob.com/sdk-sms-android-3-0-0/)
5.布局代码
xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:orientation="vertical"> <android.support.v7.widget.Toolbar style="@style/ToolBar"> <RelativeLayout style="@style/ToolBarContainerRelative"> <ImageButton style="@style/ToolBarBackButton" /> <TextView style="@style/ToolBarTitle" android:text="@string/login_phone_title" android:textColor="@color/top_bar_text_color" /> RelativeLayout> android.support.v7.widget.Toolbar> <RelativeLayout android:id="@+id/login_country_layout" android:layout_width="match_parent" android:layout_height="48dp" android:orientation="horizontal" android:paddingLeft="13dp" android:paddingRight="13dp"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:gravity="center" android:text="@string/login_country" android:textSize="@dimen/text_size_md_subheading" /> <TextView android:id="@+id/login_country_tx" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentRight="true" android:gravity="center" android:text="@string/login_country_act_title" android:textColor="@color/top_bar_text_color" android:textSize="@dimen/text_size_md_subheading" /> RelativeLayout> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:background="@color/edit_hint_color" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="15dp" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:layout_marginTop="15dp" android:paddingLeft="13dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="@dimen/text_size_md_subheading" android:text="+"/> <TextView android:id="@+id/login_country_code" style="@style/LoginEdit" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@null" android:text="86" android:digits="1234567890" android:maxLength="4" android:minLines="1" /> <EditText android:id="@+id/login_name" style="@style/LoginEdit" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:digits="1234567890" android:maxLength="11" android:background="@null" android:inputType="number" android:hint="@string/login_name_tip" /> LinearLayout> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:background="@color/edit_hint_color" /> <LinearLayout android:layout_width="match_parent" android:layout_height="48dp" android:orientation="horizontal" android:visibility="visible" > <EditText android:id="@+id/login_captcha" style="@style/LoginEdit" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:layout_weight="1" android:drawablePadding="10dp" android:hint="@string/login_captcha_tip" android:inputType="number" android:background="@null" android:maxLength="4" android:paddingTop="20dp" /> <Button android:id="@+id/login_captcha_countdown" style="@style/PrimaryButton_get" android:layout_gravity="center" android:layout_marginRight="@dimen/activity_horizontal_margin" android:text="@string/login_captcha_get" android:textColor="@color/colorPrimaryDark" /> LinearLayout> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:background="@color/edit_hint_color" /> <Button android:id="@+id/login_btn" android:layout_marginTop="40dp" android:text="@string/login" android:textColor="@color/colorPrimaryDark" style="@style/login_btn_style" /> LinearLayout>
//JAVA代码
package com.like.smartplug; import android.content.Intent; import android.os.Bundle; import android.os.CountDownTimer; import android.os.Handler; import android.os.Message; import android.text.TextUtils; import android.util.Log; import android.widget.Button; import android.widget.EditText; import com.jakewharton.rxbinding.view.RxView; import org.json.JSONObject; import java.util.concurrent.TimeUnit; import Base.BaseActivity; import cn.smssdk.EventHandler; import cn.smssdk.SMSSDK; import cn.smssdk.utils.SMSLog; import rx.functions.Action1; import util.UtilCommon; import static com.like.smartplug.R.id.login_btn; /** * Created by like on 2017/5/17. */ public class SmsLoginActivity extends BaseActivity { private EditText edit_phone_login,edit_verify_code; private Button btn_get_verify,btn_commit_verify_code; private boolean flag = true; private CountDownTimer countDownTimer;//倒计时 @Override public int getLayoutId() { return R.layout.activity_smslogin; } @Override public void init() { SMSSDK.initSDK(this, "1df0a48ca2b91", "053c87f0fecab8479fa9193386b69f3c"); EventHandler eh=new EventHandler(){ @Override public void afterEvent(int event, int result, Object data) { Message msg = new Message(); msg.arg1 = event; //事件类型 msg.arg2 = result; //操作结果,为SMSSDK.RESULT_COMPLETE表示成功,为SMSSDK.RESULT_ERROR表示失败 msg.obj = data; //操作结果,如果result=SMSSDK.RESULT_ERROR,则类型为Throwable,如果result=SMSSDK.RESULT_COMPLETE需根据event判断: handlersm.sendMessage(msg); //发送消息,等待UI处理 } }; SMSSDK.registerEventHandler(eh); //注册短信回调 } @Override public void findid(Bundle savedInstanceState) { edit_phone_login= (EditText) findViewById(R.id.login_name); edit_verify_code= (EditText) findViewById(R.id.login_captcha); btn_get_verify= (Button) findViewById(R.id.login_captcha_countdown); btn_commit_verify_code= (Button) findViewById(login_btn); } @Override public void setlistener() { RxView.clicks(btn_get_verify).throttleFirst(UtilCommon.VIEW_THROTTLE_TIME, TimeUnit.MILLISECONDS) .subscribe(new Action1//这是2.1.4.版的,最新的SDK请参照官网开发指南进行集成,写这篇博客主要是想把这个思路写一下。看下效果图() { @Override public void call(Void aVoid) { String phone=edit_phone_login.getText().toString().trim(); if (TextUtils.isEmpty(phone)){ shortToast(getResources().getString(R.string.phone_null)); return; } else if (!judgePhoneNumss(phone)){ return; } SMSSDK.getVerificationCode("86",phone); edit_verify_code.requestFocus(); countDownTimer = new CountDownTimer(60000, 1000) { @Override public void onTick(long millisUntilFinished) { btn_get_verify.setText(getString(R.string.login_captcha_countdown, millisUntilFinished / 1000)); } @Override public void onFinish() { btn_get_verify.setText(R.string.login_captcha_get); countDownTimer = null; } }.start(); } }); RxView.clicks(btn_commit_verify_code).throttleFirst(UtilCommon.VIEW_THROTTLE_TIME,TimeUnit.MILLISECONDS) .subscribe(new Action1 () { @Override public void call(Void aVoid) { if (TextUtils.isEmpty(edit_verify_code.getText().toString())) { shortToast(getResources().getString(R.string.verify_code_null)); return; } String phone=edit_phone_login.getText().toString().trim(); String verifycode=edit_verify_code.getText().toString().trim(); SMSSDK.submitVerificationCode("86",phone,verifycode); showLoadingDialog(); } }); } //UI线程处理handler的消息,这个handler用于接收短信回调事件 Handler handlersm=new Handler(){ @Override public void handleMessage(Message msg) { 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) { if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE)//提交验证码成功,验证通过 { Log.i("info", "handleMessage: "+getResources().getString(R.string.verify_succ)); dismissLoadingDialog(); Intent intent=new Intent(getApplication(),MainActivity.class); startActivity(intent); finish(); } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE)//服务器验证码发送成功 { Log.i("info", "handleMessage: "+getResources().getString(R.string.verify_send)); shortToast(getResources().getString(R.string.verify_send)); } else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES)//返回支持发送验证码的国家列表 { shortToast(getResources().getString(R.string.get_country_code_succ)); } } else { int status=0; try { dismissLoadingDialog(); ((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)) { shortToast(des); return; } } catch (Exception e) { SMSLog.getInstance().w(e); } } } }; //判断手机号码是否合理 public boolean judgePhoneNumss(String number) { if (isMatchLength(number, 11) && isMobileNO(number)) { return true; } shortToast(getResources().getString(R.string.input_phone_error)); return false; } //判断一个字符串的位数 public static boolean isMatchLength(String str, int length) { if (str.isEmpty()) { return false; } else { return str.length() == length ? true : false; } } //验证手机格式 public static boolean isMobileNO(String mobileNums) { /* * 移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188 * 联通:130、131、132、152、155、156、185、186 电信:133、153、180、189、(1349卫通) * 总结起来就是第一位必定为1,第二位必定为3或5或8,其他位置的可以为0-9 */ String telRegex = "[1][3578]\\d{9}";// "[1]"代表第1位为数字1,"[358]"代表第二位可以为3、5、8中的一个,"\\d{9}"代表后面是可以是0~9的数字,有9位。 if (TextUtils.isEmpty(mobileNums)) return false; else return mobileNums.matches(telRegex); } @Override protected void onDestroy() { super.onDestroy(); SMSSDK.unregisterAllEventHandler(); } }