Basic principle: Through the Contentobserver Observer listens to the SMS database change, when has the change detects whether is from the specified number the text message, if is the word to obtain the necessary content through the regular expression, then the code (*^__^*) hehe ... :
1 /**2 * 3 * @ClassName: Smscontent4 * @Description: SMS Listening class5 * @authorGuoyizhe6 * @email [email protected]7 * @date 2015-6-9 pm 3:30:128 *9 */Ten Public classSmscontentextendsContentobserver { One A Public Static FinalString Sms_uri_inbox = "Content://sms/inbox"; - PrivateActivity activity =NULL; - PrivateString smscontent = ""; the PrivateEditText Verifytext =NULL; - PrivateHandler Handler; - Private intSms_content = 1; - + Publicsmscontent (activity activity, Handler Handler, EditText verifytext) { - Super(handler); + This. Activity =activity; A This. Verifytext =Verifytext; at This. Handler =handler; - } - - @Override - Public voidOnChange (BooleanSelfchange) { - Super. OnChange (selfchange); incursor cursor =NULL; - //read an unread text message of the specified number in the Inbox, generally only listen to your company's SMS channel number, if there are multiple, the query statement to address in (XXX,XXX) can be tocursor = Activity.getcontentresolver (). Query (Uri.parse (Sms_uri_inbox),NewString[] {"_id", "address", "body", "read"}, "address =?" and read=? ", + NewString[] {"5554", "0"}, "Date desc"); - if(Cursor! =NULL) {//have unread text messages the Cursor.movetofirst (); * if(Cursor.movetofirst ()) {//here is also for one, multiple words using while loop traversal can be $String smsbody = cursor.getstring (Cursor.getcolumnindex ("Body"));Panax NotoginsengString regEx = "[^0-9]"; -Pattern p =Pattern.compile (regEx); theMatcher m =P.matcher (smsbody.tostring ()); +Smscontent = M.replaceall (""). Trim (). toString (); A Verifytext.settext (smscontent); the } + //Many other transactions can be handled through handler, here's just a chestnut. - handler.obtainmessage (Sms_content, Verifytext.gettext (). toString ()). Sendtotarget (); $ cursor.close (); $ } - } -}
Then the main activity:
1 /**2 * 3 * @ClassName: mainactivity4 * @Description: Listen to incoming SMS and autofill, or handle some events5 * @authorGuoyizhe6 * @email [email protected]7 * @date 2015-6-9 pm 2:17:568 *9 */Ten Public classMainactivityextendsActivity { One PrivateHandler Handler =NewHandler () { A Public voidhandlemessage (android.os.Message msg) { - Switch(msg.what) { - Case1://handling messages after the arrival event the //TODO - Break; - } - }; + }; - + @Override A protected voidonCreate (Bundle savedinstancestate) { at Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -EditText Text =(EditText) Findviewbyid (r.id.smsobserver); -Smscontent content =NewSmscontent (mainactivity. This, handler, text); - //Register SMS Change monitor - This. Getcontentresolver (). Registercontentobserver (Uri.parse ("content://sms/"),true, content); in } -}
Android Monitor SMS arrives and automatically fills in the input box