android 短信验证自动获取验证码
权限:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
activity类
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.SmsMessage;
import android.text.TextUtils;
import android.widget.EditText;
@SuppressLint("HandlerLeak")
public class MainActivity extends Activity {
private BroadcastReceiver smsReceiver;
private IntentFilter filter2;
private Handler handler;
private EditText et;
private String strContent;
private String patternCoder = "(?<!\\d)\\d{6}(?!\\d)";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.et);
handler = new Handler() {
public void handleMessage(android.os.Message msg) {
et.setText(strContent);
};
};
filter2 = new IntentFilter();
filter2.addAction("android.provider.Telephony.SMS_RECEIVED");
filter2.setPriority(Integer.MAX_VALUE);
smsReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Object[] objs = (Object[]) intent.getExtras().get("pdus");
for (Object obj : objs) {
byte[] pdu = (byte[]) obj;
SmsMessage sms = SmsMessage.createFromPdu(pdu);
// 短信的内容
String message = sms.getMessageBody();
// 短息的手机号
String from = sms.getOriginatingAddress();
if (!TextUtils.isEmpty(from)) {
String code = patternCode(message);
if (!TextUtils.isEmpty(code)) {
strContent = code;
handler.sendEmptyMessage(1);
}
}
}
}
};
registerReceiver(smsReceiver, filter2);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(smsReceiver);
}
/**
* 匹配短信中间的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;
}
}
原文地址:http://blog.csdn.net/menglele1314/article/details/45966109