做了很多实验,看了很多demo,网上几乎都是发送消息然后自定义获取验证码,可是实际开发中,我们最常见的还是通过发送请求而获得的验证码:
下面贴出例子,我这里用的请求框架是AsyncHttp,各位可以随便,这里的接口暂时还能用,各位可以参考;
注意:我这里后台拿的code字段是我自己服务器返回的,你要根据实际情况你后台给你返回的字段要匹配
activity_main:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:layout_gravity="center_vertical"
android:id="@+id/phone"
style="?android:attr/textViewStyle"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="@drawable/bg_edittext"
android:hint="手机号"
android:inputType="number"
android:singleLine="true"
android:textSize="20sp" />
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:layout_gravity="center_vertical"
android:id="@+id/id_verify_number_editview"
style="?android:attr/textViewStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/bg_edittext"
android:hint="@string/wait_write_sms"
android:inputType="number"
android:singleLine="true"
android:textSize="20sp" />
<Button
android:layout_gravity="center_vertical"
android:id="@+id/id_verify_number_sendbutton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/receive_sms" />
</LinearLayout>
</LinearLayout>
MianActivity:
package com.example.verifysms;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.telephony.SmsMessage;
import android.text.TextUtils;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText mEditText,phone;
private Button mSendButton;
private String getcodeURL = "http://12********************/sms";
private AsyncHttpClient client = new AsyncHttpClient();
//短信验证码内容 验证码是6位数字的格式
private String strContent;
private String patternCoder = "(?<!\\d)\\d{6}(?!\\d)";
//更新界面
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (mEditText != null) {
mEditText.setText(strContent);
}
}
};
public void getCode() {
HashMap<String, String> map = new HashMap<String, String>();
map.put("phone", phone.getText().toString().trim());
RequestParams params = new RequestParams(map);
client.post(getcodeURL, params,
new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers,
JSONObject response) {
Log.e("", response.toString());
super.onSuccess(statusCode, headers, response);
Toast.makeText(MainActivity.this, "短信已成功发送,请注意查收", 1).show();
try {
String message=response.getString("code");
//这个方法是正则表达式,但是因为我后台返回的是空,所以我这里正则表达式就暂时注释掉了,如果你后台是可以打印出验证码的这里你可以打开
// String code = patternCode(message);
if (!TextUtils.isEmpty(message)) {
strContent = message;
mHandler.sendEmptyMessage(1);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers,
String responseString, Throwable throwable) {
super.onFailure(statusCode, headers, responseString, throwable);
}
@Override
public void onFailure(int statusCode, Header[] headers,
Throwable throwable, JSONObject errorResponse) {
super.onFailure(statusCode, headers, throwable,
errorResponse);
}
});
}
//监听短信广播
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mEditText = (EditText) findViewById(R.id.id_verify_number_editview);
phone=(EditText) findViewById(R.id.phone);
mSendButton = (Button) findViewById(R.id.id_verify_number_sendbutton);
mSendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
getCode();
}
});
}
/**
* 匹配短信中间的6个数字(验证码等)
*
* @param patternContent
* @return
*/
private String patternCode(String patternContent) {
if (TextUtils.isEmpty(patternContent)) {
return null;
}
Pattern p = Pattern.compile(patternCoder);
Matcher matcher = p.matcher(patternContent);
if (matcher.find()) {
return matcher.group();
}
return null;
}
}