Read and intercept SMS messages in Android

Source: Internet
Author: User

In Android, SMS is also a type of broadcast. When a text message is sent, the system sends a broadcast. We only need to process broadcast events to read text messages.

Read text message code 1:

Public class smsreceiver extends broadcastreceiver {<br/> Public final static smsmessage [] getmessagesfromintent (intent) {<br/> bundle = intent. getextras (); <br/> Object messages [] = (object []) bundle. get ("PDUS"); <br/> smsmessage msmsmessage [] = new smsmessage [messages. length]; <br/> for (INT I = 0; I <messages. length; I ++) {<br/> msmsmessage [I] = smsmessage. createfrompdu (byte []) messages [I]); <br/>}</P> <p> return msmessage; </P> <p >}< br/>}

Read text message code 2:

/*** Get sms ** @ Param intent * @ return */public final static smsmessage [] getmessagesfromintent (intent) {object [] messages = (object []) intent. getserializableextra ("PDUS"); byte [] [] pduobjs = new byte [messages. length] []; for (INT I = 0; I <messages. length; I ++) {pduobjs [I] = (byte []) messages [I];} byte [] [] PDUS = new byte [pduobjs. length] []; int pducount = PDUS. length; smsmessage [] msgs = new smsmessage [pducount]; for (INT I = 0; I <pducount; I ++) {PDUS [I] = pduobjs [I]; msgs [I] = smsmessage. createfrompdu (PDUS [I]);} return msgs ;}

 

Note that the permission must be declared in androidmanifest. xml.

 <Cycler Android: Name = ". smsreceiver "Android: enabled =" true "> <br/> <intent-filter> <br/> <action Android: Name =" android. provider. telephony. sms_received "/> <br/> </intent-filter> <br/> </Cycler> <br/>

The preceding content is to be continued ....

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.