Overview
There are two common methods to automatically fill in SMS verification codes on Android. One is to use BroadCastReceiver, and the other is to monitor changes in the SMS database on the mobile phone. Using BroadCastReceiver to implement it will be invalid in some cases. The most common thing is that when software with spam blocking is installed on your phone, the automatic filling of the SMS verification code is invalid. Therefore, now we generally use the method of monitoring changes in database content to automatically fill in SMS verification code.
There are many articles online that use monitoring database content changes to automatically fill in SMS verification codes, which are mainly divided into the following steps:
1. Inherit ContentObserver to implement an observer of a SMS database, and then onChange.
2. Register an observer in the Activity.
3. Add relevant permissions in the AndroidManifast file.
4. Don't forget to unregister the observer in the onDestroy method of the Activity.
This method has been proven to be feasible and is easier to use. This method reads the Cursor source of the SMS and the managedQuery method of the Activity. This method has been marked as discarded and it is recommended to use CursorLoader instead.
Benefits of CursorLoader
First of all, CursorLoader inherits AsyncTaskLoader, which uses asynchronous methods to query the database, avoiding the problem of synchronous query blocking UI threads. It will monitor changes in the data source and will report it actively. Moreover, when a configuration change occurs, the regenerated loader will automatically connect to the cursor before the change, and can be automatically released when the query resource is not used.
Implementation class for automatic filling of SMS verification code
If you have a disagreement, you will directly post the code:
package ; import ; import ; import ; import ; import ; import ; import .; import .; import .; import ; import ; import ; /** * <p>Author: Wuwang </p> * <p>Company : ZhiJiaKeJi</p> * <p>Date :2016-07-21 09:16</p> * <p>Description: Automatically fill the verification code</p> */ public class VerificationCodeAutoFill implements <Cursor> { private Context context; private OnNewMessageListener listener; private String msgRegular; public VerificationCodeAutoFill(Context context){ this(context,null,null); } public VerificationCodeAutoFill(Context context,OnNewMessageListener listener){ this(context,listener,null); } /** * @param context * @param listener New SMS Monitor * @param msgRegular Regular expression for processing new text messages, if empty, it will not be processed */ public VerificationCodeAutoFill(Context context, OnNewMessageListener listener,String msgRegular){ =context; =listener; =msgRegular; } public VerificationCodeAutoFill registerTo(int id,LoaderManager manager){ (id,null,this); return this; } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { return new CursorLoader(context,("content://sms/inbox"), new String[] { "_id", "address", "read", "body" }, " read=?", new String[] {"0" }, "_id desc"); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { // Sort by id. If you sort by date, the text messages you read will not be accurate after modifying the phone time. if (data != null && () > 0) { ContentValues values = new ContentValues(); ("read", "1"); // Modify the SMS to read mode (); int smsbodyColumn = ("body"); String smsBody = (smsbodyColumn); ("wuwang",smsBody); if(listener!=null){ (getDynamicPassword(smsBody,msgRegular)); } } } @Override public void onLoaderReset(Loader<Cursor> loader) { } public interface OnNewMessageListener { void onNewMessage(String msg); } private String getDynamicPassword(String str,String regular) { if(regular==null)return str; Pattern continuousNumberPattern = (msgRegular); Matcher m = (str); String dynamicPassword = ""; while (()) { (()); dynamicPassword = (); } return dynamicPassword; } }
How to use
Use is relatively simple, just add it to FragmengActivity:
//SMS verification code matching four digitsnew VerificationCodeAutoFill(this, new () { @Override public void onNewMessage(String msg) { //(msg); //Fill in verification code } }, "\\d{4}").registerTo(1,getSupportLoaderManager());
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.