First, this is the flow of this stuff:
Whether you use mob or mobile or your own SMS interface is the same, in the sending verification code part to request, and then the server returned to you a short message, and then you deal with it, to achieve the effect.
This is the only need to listen to the database changes in the text message can be, and to listen to the one you want.
First,
Contentobserver
import android.annotation.suppresslint;import android.content.context;import android.database.contentobserver;import android.database.cursor;import android.net.uri;import android.os.handler;import android.provider.settings;import android.util.log;import Java.util.regex.matcher;import java.util.regex.pattern;public class smsobserver extends ContentObserver { private Context mContext; private handler mhandler; public smsobserver (Context context, handler handler) { super (Handler); mContext = context; mHandler = handler; } @SuppressLint ("Newapi") @Override p Ublic void onchange (Boolean selfchange, uri uri) { super.onchange (Selfchange, uri); LOG.E ("DEBUG", "sms has changed!");         LOG.E ("DEBUG", uri.tostring ());// try {// Int isairplaneopen = settings.system.getint (Mcontext.getcontentresolver (), Settings.System.AIRPLANE_MODE_ON);// LOG.I ("Strange", " isAirplaneOpen -----> " + isairplaneopen);// mhandler.obtainmessage (LoginActivity.MSG_RECEIVED_CODE, isairplaneopen)// .sendtotarget ();// } catch (settings.settingnotfoundexception e) {// e.printstacktrace ();// } String code = ""; if (Uri.tostring () equals ("Content://sms/raw")) { return; } string[] projection = new string[]{"_id", "Address", "body", "type"}; uri inboxuri = uri.parse ("Content://sms/inbox"); cursor c = mcontext.getcontentresOlver (). Query (inboxuri, projection, null, null, "Date desc"); if (c != null) { if (C.movetofirst ()) { int _id = c.getint (C.getcolumnindex ("_id")); String type = C.getstring (C.getcolumnindex ("type")); string address = c.getstring (C.getcolumnindex ("Address")); string body = c.getstring (C.getcolumniNdex ("Body")); LOG.E ("--------address------", address);       LOG.V ("-----Grass-----", "msgid : " + _id);                LOG.V ("-----Grass-----", "msgaddr : " + type);       LOG.V ("-----Grass-----", "msgbody : " + body);                LOG.V ("-----Grass-----", "msgtype : " + address); if (!address.contains ("10657120610111") { return;//10657120610111 }             LOG.E ("DEBUG", "Sender:" + address + " " + "SMS Content:" + body); pattern pattern = pattern.compile (" (\\d{6}); Matcher matcher = pattern.matcher (body); if (Matcher.find ()) { code = Matcher.group (0); LOG.E ("DEBUG", "code is " + code); mhandler.obtainmessage ( Loginactivity.msg_received_code, code) . Sendtotarget (); } } c.close (); } }}
This is the observer pattern of the class, the role is to listen for text messages, according to its changes to obtain the content of the text message, and according to the regular expression to intercept the desired number, and then handle a bit.
Then the
Mainactivity
private smsobserver mobserver;public static final int msg_received_code = 1; @SuppressLint ("Handlerleak") Private handler mhandler2 = new handler () { @Override public void handlemessage (message msg) { if (Msg.what == msg_received_code) { string code = (String) msg.obj; // update the ui inputcodeet.settext (code); } }}; @Overrideprotected void OnCreate (bundle savedinstancestate) { super.oncreate (savedInstanceState); &nbsP;setcontentview (R.layout.activity_login); init (); mobserver = new smsobserver (loginactivity.this, mhandler2); uri uri = uri.parse ("content://sms"); getcontentresolver (). RegisterContentObserver ( Uri, true, mobserver);} @Overrideprotected void ondestroy () { getcontentresolver (). Unregistercontentobserver (Mobserver); super.ondestroy ();}
Register for monitoring in Main, don't forget to destroy the last
In this way, in the monitoring state will be in real-time on the transmission of the text messages to listen, and get the number we want, through the sub-thread update EditText to achieve the effect of automatic filling.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/74/3C/wKioL1YXNMWAETzEAACS_-lSRUI836.jpg "title=" Rp]{hu _7@126fjg[@{$W ([b.png "alt=" Wkiol1yxnmwaetzeaacs_-lsrui836.jpg "/>
Finally summed up, generally like short messages, flight mode, what settings and other suggestions with contentobserver, like what push, update, video chat, suggest with service, reasonable allocation of memory use, optimize the performance of the app.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/74/3D/wKioL1YXNbSyxrjtAAQ8zhtm9UY741.gif "title=" C2adb464tw1etn3dny1qng208c04p7bj.gif "alt=" Wkiol1yxnbsyxrjtaaq8zhtm9uy741.gif "/>
Address: http://down.51cto.com/data/2103679
This article is from the "Liangxiao Technology Center" blog, please be sure to keep this source http://liangxiao.blog.51cto.com/3626612/1701144
Android Studio Phase II-Text message auto-fill project Flow explained