Android免费获取短信验证码
最后更新于:2022-04-01 14:25:46
Android在应用注册的时候经常要用到短信验证,通过发短信给用户,并让用户将短信中的验证码填到注册页面中方能注册成功。如下图所示:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-04-08_570771b47b88b.jpg)
获取到短信如下:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-04-08_570771b491ac7.jpg)
本文是能过第三方平台掌淘科技来实现免费短信验证。首先到注册一个账号,网址是[http://mob.com/](http://mob.com/);注册完后就可以得到appkey和appsecret,如下图:
点击进入后就能看到自己的appkey和appsecret。接下来就要导入免费短信验证的sdk,同样在那个网站上下载。下载完后把文件夹MSMSDK文件导入到eclipse中,把它作为一个library导入到你的工程中,接下来就是代码的实现。
1.在AndroidMainfest导入权限:
~~~
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
~~~
2.在application中加入如下的activity声明:
~~~
<activity
android:name="cn.smssdk.SMSSDKUIShell"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:windowSoftInputMode="stateHidden|adjustResize" />
~~~
3.实现注册页面的xml,如下,主要就是两个文本框和一个登录按钮。
~~~
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:paddingLeft="10.0dip"
android:paddingRight="10.0dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30.0dip">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:orientation="vertical"
android:paddingLeft="10.0dip"
android:paddingTop="20.0dip"
android:paddingRight="10.0dip"
android:paddingBottom="20.0dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/table_top_xml">
<EditText
android:id="@+id/register_username"
android:textSize="15.0sp"
android:textColor="#000000"
android:gravity="center_vertical"
android:background="@null"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10.0dip"
android:layout_marginRight="30.0dip"
android:hint="手机号"
android:singleLine="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<RelativeLayout
android:orientation="vertical"
android:paddingLeft="10.0dip"
android:paddingTop="15.0dip"
android:paddingRight="10.0dip"
android:paddingBottom="15.0dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/table_middle_xml">
<EditText
android:id="@+id/register_yanzhengma"
android:textSize="15.0sp"
android:gravity="center_vertical"
android:background="@null"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10.0dip"
android:layout_marginRight="30.0dip"
android:hint="验证码"
android:singleLine="true"
android:layout_centerVertical="true" />
<TextView
android:textSize="13.0sp"
android:textColor="#ffd74623"
android:id="@+id/register_getyanzhengma"
android:background="@drawable/btn_register"
android:paddingLeft="10.0dip"
android:paddingTop="5.0dip"
android:paddingRight="10.0dip"
android:paddingBottom="5.0dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取"
android:singleLine="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<Button
android:id="@+id/register_ok"
android:layout_width="match_parent"
android:layout_height="50.0dp"
android:layout_marginTop="20dp"
android:background="@drawable/login_try_btn"
android:gravity="center"
android:text="注 册"
android:textColor="@android:color/white"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
~~~
4.接下来就是在主函数了,
~~~
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phonEditText=(EditText) findViewById(R.id.register_username);
verEditText=(EditText) findViewById(R.id.register_yanzhengma);
SMSSDK.initSDK(this,APPKEY,APPSECRET);
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;
handler.sendMessage(msg);
}
};
SMSSDK.registerEventHandler(eh);
findViewById(R.id.register_getyanzhengma).setOnClickListener(clickListener);
}
~~~
以上SMSSDK.initSDK是引入SMSSDK,APPKEY和APPSECRET写入你自己的appkey和appsecret。接下来的EventHander主要是获得SMSSDK的状态。接下来实现点击获取按钮的监听事件。
~~~
OnClickListener clickListener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.register_getyanzhengma: //获取验证码
if(!TextUtils.isEmpty(phonEditText.getText().toString())){
SMSSDK.getVerificationCode("86",phonEditText.getText().toString());
}else {
Toast.makeText(MainActivity.this, "电话不能为空", 1).show();
}
break;
case R.id.register_ok:
if(!TextUtils.isEmpty(verEditText.getText().toString())){
SMSSDK.submitVerificationCode("86", phonEditText.getText().toString(), verEditText.getText().toString());
}else {
Toast.makeText(MainActivity.this, "验证码不能为空", 1).show();
}
default:
break;
}
}
};
~~~
最后就是判断短信发送所处的状态。
~~~
Handler handler=new Handler(){
@Override
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) {
//短信注册成功后,返回MainActivity,然后提示新好友
if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {//提交验证码成功
Toast.makeText(getApplicationContext(), "提交验证码成功", Toast.LENGTH_SHORT).show();
} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
Toast.makeText(getApplicationContext(), "验证码已经发送", Toast.LENGTH_SHORT).show();
}
} else {
((Throwable) data).printStackTrace();
Toast.makeText(getApplicationContext(), "验证码错误", Toast.LENGTH_SHORT).show();
}
}
};
~~~
以上的实现方式是通过SMSSDK的接口来实现的,如果有兴趣可以细读SMSSDK里的源码。在此只是把它实现出来。到此一个简单的注册短信验证就实现了。当然短信的格式修改不了。如果要自定义短信格式,那就需要找收费的咯。