在现如今,随着互联网时代飞速的发展和进步,目前大部分软件里面注册信息,都需要短信验证吗功能,同时为了避免用户自己输入导致的繁琐操作,有一部分app设计者将其设置成了自动填充的方式,而今天扣丁学堂给大家分享了Android培训之自动填充短信验证码的具体代码,首先利用广播接收器来拦截短信获取其中匹配的内容,提供回掉,将短信内容暴露到activity中实现自动填充,下面我们一起来看一下吧。
首先我们要实现一个广播接收器
packagecom.wquant.weilt.reciver;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
importandroid.content.BroadcastReceiver;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.os.Bundle;
importandroid.os.Message;
importandroid.telephony.SmsMessage;
importandroid.text.TextUtils;
importandroid.util.Log;
publicclassSmsReciverextendsBroadcastReceiver{
privateStringpatternCoder="(?
@Override
publicvoidonReceive(Contextcontext,Intentintent){
//获取短信数据
Object[]objs=(Object[])intent.getExtras().get("pdus");
for(Objectobj:objs){
byte[]pdu=(byte[])obj;
//将字节数组封装成为smsmessage对象
SmsMessagesms=SmsMessage.createFromPdu(pdu);
//获得短短信内容
Stringmessage=sms.getMessageBody();
Log.d("短信内容","message:"+message);
//短息的手机号。。+86开头?
Stringfrom=sms.getOriginatingAddress();
Log.d("短信来源","from:"+from);
if(!TextUtils.isEmpty(from)){
Stringcode=patternCode(message);
if(!TextUtils.isEmpty(code)){
mMessageListener.onReceived(code);
}
}
}
}
/**
*匹配短信中间的6个数字(验证码等)
*
*@parampatternContent
*@return
*/
privateStringpatternCode(StringpatternContent){
if(TextUtils.isEmpty(patternContent)){
returnnull;
}
Patternp=Pattern.compile(patternCoder);
Matchermatcher=p.matcher(patternContent);
if(matcher.find()){
returnmatcher.group();
}
returnnull;
}
//回调接口
publicinterfaceMessageListener{
publicvoidonReceived(Stringmessage);
}
MessageListenermMessageListener;
publicvoidsetOnReceivedMessageListener(MessageListenermessageListener){
this.mMessageListener=messageListener;
}
}
ok上面我们已经实现了广播接收器,在activity中我们要绑定一个意图过滤器并将此广播注册在destory方法中要将其解除注册
packagecom.wquant.weilt.controler;
importorg.apache.http.Header;
importorg.json.JSONException;
importorg.json.JSONObject;
importandroid.content.Intent;
importandroid.content.IntentFilter;
importandroid.os.Bundle;
importandroid.os.CountDownTimer;
importandroid.view.KeyEvent;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.ImageView;
importandroid.widget.TextView;
importandroid.widget.Toast;
importcom.loopj.android.http.RequestParams;
importcom.wquant.weilt.MyApplication;
importcom.wquant.weilt.R;
importcom.wquant.weilt.control.base.JsonHttpResponseHandlerBase;
importcom.wquant.weilt.control.base.MyBaseActivity;
importcom.wquant.weilt.reciver.SmsReciver;
importcom.wquant.weilt.reciver.SmsReciver.MessageListener;
importcom.wquant.weilt.util.CToast;
importcom.wquant.weilt.util.CommonUtil;
importcom.wquant.weilt.util.Constant;
importcom.wquant.weilt.util.HttpUtil;
/**
*修改密码
*
*@authorzhaomy
*
*/
publicclassRestartLoginOrTradPwdActivityextendsMyBaseActivity{
SmsReciverreciver;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
//TODOAuto-generatedmethodstub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restart_login_or_trad);
init();
}
privatevoidinit(){
code=(EditText)findViewById(R.id.register_code);
reciver=newSmsReciver();
IntentFilterfilter=newIntentFilter();
//设置短信拦截参数
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
//设置最大优先级
filter.setPriority(Integer.MAX_VALUE);
registerReceiver(reciver,filter);
reciver.setOnReceivedMessageListener(newMessageListener(){
@Override
publicvoidonReceived(Stringmessage){
code.setText(message);
}
});
}
@Override
protectedvoidonPause(){
super.onPause();
}
@Override
protectedvoidonDestroy(){
unregisterReceiver(reciver);
super.onDestroy();
}
}
网友评论