Android Monitor SMS arrives and automatically fills in the input box

Source: Internet
Author: User

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

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.