Android uses Cursorloader to realize automatic message verification code filling _android

Source: Internet
Author: User

Overview

Android on the implementation of text message verification code automatically fill out, commonly used in two ways. One is the use of broadcastreceiver, and one is to monitor the mobile phone text message database changes. The use of broadcastreceiver to achieve will be in some cases ineffective, the most common is the mobile phone has been installed with spam message interception function of the software, SMS Verification code automatically fill invalid. Therefore, now generally choose to listen to the changes in the content of the database to achieve the automatic message verification code fill.
Online for the use of listening to the content of the database changes to achieve the automatic message verification code to fill in the article is also a lot, mainly divided into steps:
1. Inherit contentobserver to realize a short message database observer, and then onchange.
2. Register an observer in the activity.
3. Add related permissions in the Androidmanifast file.
4. Do not forget to ondestory the observer who has been registered in the method of activity.
This method is proven to be feasible and easy to use. This method reads the cursor source of the SMS and the Managedquery method of the activity, which has been marked as obsolete and recommended for Cursorloader instead.

The benefits of Cursorloader

First, Cursorloader inherits the Asynctaskloader, querying the database asynchronously, avoiding the problem of a synchronous query blocking the UI thread. It will monitor changes to the data source itself and will proactively escalate. Also, when configuration changes occur, the regenerated loader automatically connects to the cursor before the change, and can be automatically freed when the queried resource is not used.

The realization class of automatic message verification code filling

A word will be directly attached to the code:

Package Com.wuwang.demo;
Import android.content.ContentValues;
Import Android.content.Context;
Import Android.database.ContentObserver;
Import Android.database.Cursor;
Import Android.net.Uri;
Import Android.os.Bundle;
Import Android.support.v4.app.LoaderManager;
Import Android.support.v4.content.CursorLoader;
Import Android.support.v4.content.Loader;

Import Android.util.Log;
Import Java.util.regex.Matcher;

Import Java.util.regex.Pattern; /** * <p>author:wuwang </p> * <p>Company:ZhiJiaKeJi</p> * <p>date:2016-07-21 09:16&L T;/p> * <p>description: Verification Code Auto Fill </p> * * public class Verificationcodeautofill implements Loadermanager.lo
  adercallbacks<cursor> {Private context context;
  Private Onnewmessagelistener listener;

  Private String msgregular;
  Public Verificationcodeautofill {This (context,null,null); } public Verificationcodeautofill (context Context,onnewmessagelistener Listener) {This (contExt,listener,null); /** * @param context * @param listener new SMS Listening * @param msgregular regular expressions for new SMS processing, if empty, do not process * * Public Ver
    Ificationcodeautofill (context context, Onnewmessagelistener listener,string msgregular) {this.context=context;
    This.listener=listener;
  This.msgregular=msgregular;
    Public Verificationcodeautofill Registerto (int Id,loadermanager manager) {Manager.initloader (id,null,this);
  return this; @Override public loader<cursor> oncreateloader (int id, Bundle args) {return new Cursorloader (Context,uri . Parse ("Content://sms/inbox"), new string[] {"_id", "Address", "read", "Body"}, "Read=?", New S
  Tring[] {"0"}, "_id desc"); @Override public void onloadfinished (loader<cursor> Loader, Cursor data) {//sorted by ID and, if sorted by date, after changing the phone time, read
      Text messages are not allowed. if (data!= null && data.getcount () > 0) {contentvalues values = new Contentvalues (); Values.put ("read", "1");
      Revise SMS to read mode Data.movetonext ();
      int smsbodycolumn = Data.getcolumnindex ("Body");
      String smsbody = data.getstring (Smsbodycolumn);
      LOG.E ("Wuwang", smsbody);
      if (listener!=null) {listener.onnewmessage (Getdynamicpassword (smsbody,msgregular)); @Override public void Onloaderreset (Loader<cursor> Loader) {} public interface Onnewmessagel
  Istener {void Onnewmessage (String msg);
    private string Getdynamicpassword (string str,string Regular) {if (regular==null) return str;
    Pattern Continuousnumberpattern = Pattern.compile (msgregular);
    Matcher m = continuousnumberpattern.matcher (str);
    String Dynamicpassword = "";
      while (M.find ()) {System.out.print (M.group ());
    Dynamicpassword = M.group ();
  return Dynamicpassword;
 }

}

How to use

The use of simple, directly fragmengactivity added:

Match four-digit SMS authentication Code
new Verificationcodeautofill (This, new Verificationcodeautofill.onnewmessagelistener () {
      @ Override public
      void Onnewmessage (String msg) {
        //etmsg.settext (msg);//Authentication code padding
      }
    , "\\d{4}"). Registerto (1,getsupportloadermanager ());

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.