Android实现短信验证码自动拦截读取功能
本文实例为大家分享了Android短信验证码自动拦截读取的具体代码,供大家参考,具体内容如下
知识准备:
1.观察者模式的理解【文章稍后来到~~】
2.Android的Cursor使用【Android基础】
3.正则表达式使用【Java基础】
4.Handler使用【Android基础】
代码整理:
MainActivity.java
importandroid.net.Uri; importandroid.os.Bundle; importandroid.os.Handler; importandroid.os.Message; importandroid.support.v7.app.AppCompatActivity; importandroid.view.Menu; importandroid.view.MenuItem; importandroid.widget.EditText; publicclassMainActivityextendsAppCompatActivity{ publicstaticfinalintMSG_RECEIVER_CODE=1; privateEditTextsmsEt; privateHandlerhandler=newHandler(){ @Override publicvoidhandleMessage(Messagemsg){ switch(msg.what){ caseMSG_RECEIVER_CODE: Stringmessage=(String)msg.obj; smsEt.setText(message); smsEt.setSelection(message.length()); break; default: break; } } }; privateMySmsObservermySmsObserver; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); smsEt=(EditText)findViewById(R.id.smsEt); mySmsObserver=newMySmsObserver(this,handler); Uriuri=Uri.parse("content://sms"); getContentResolver().registerContentObserver(uri,true,mySmsObserver); } @Override publicbooleanonCreateOptionsMenu(Menumenu){ getMenuInflater().inflate(R.menu.menu_main,menu); returntrue; } @Override protectedvoidonDestroy(){ super.onDestroy(); getContentResolver().unregisterContentObserver(mySmsObserver); } @Override publicbooleanonOptionsItemSelected(MenuItemitem){ intid=item.getItemId(); if(id==R.id.action_settings){ returntrue; } returnsuper.onOptionsItemSelected(item); } }
MySmsObserver.java
importandroid.content.Context; importandroid.database.ContentObserver; importandroid.database.Cursor; importandroid.net.Uri; importandroid.os.Handler; importandroid.util.Log; importjava.util.regex.Matcher; importjava.util.regex.Pattern; /** *CreatedbyNateon2015/9/28. */ publicclassMySmsObserverextendsContentObserver{ privateContextmContext; privateHandlermHandler; publicMySmsObserver(Contextcontext,Handlerhandler){ super(handler); this.mContext=context; this.mHandler=handler; } @Override publicvoidonChange(booleanselfChange,Uriuri){ super.onChange(selfChange,uri); if(uri.toString().equals("content://sms/raw")){ return; } UriqueryUri=Uri.parse("content://sms/inbox"); Stringcode=""; Cursorcursor=mContext.getContentResolver().query(queryUri,null,null,null,"datedesc"); if(cursor!=null){ if(cursor.moveToFirst()){ Stringaddress=cursor.getString(cursor.getColumnIndex("address")); Stringmessage=cursor.getString(cursor.getColumnIndex("body")); //TODO:2015/9/28这里可以根据address做一些自己的判断,比如只有特定的手机号才做判断 Log.e("guxuewu","address:==>"+address+"message:==>"+message); //TODO:2015/9/28这里可以根据自己的项目进行特定的正则表达式的编写 Patternpattern=Pattern.compile("(\\d{6})"); Matchermatcher=pattern.matcher(message); if(matcher.find()){ code=matcher.group(0); mHandler.obtainMessage(MainActivity.MSG_RECEIVER_CODE,code).sendToTarget(); } } cursor.close(); } } }
activity_main.xml
<RelativeLayoutxmlns: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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <EditText android:id="@+id/smsEt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:ems="10"/> </RelativeLayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。