Android SMS send and receive (ii)

Source: Internet
Author: User

Receive the SMS class with the following code

For receive SMS private Smsreceiver msmsreceiver;        @Override protected void Onresume () {super.onresume ();        Msmsreceiver = new Smsreceiver ();        Intentfilter intentfilter = new Intentfilter ();        Intentfilter.addaction ("Android.provider.Telephony.SMS_RECEIVED");        Intentfilter.setpriority (1000);    Registerreceiver (Msmsreceiver, Intentfilter);        } @Override protected void OnPause () {super.onpause ();    Unregisterreceiver (Msmsreceiver); } public class Smsreceiver extends Broadcastreceiver {public static final String sms_received_action = "Android . Provider.        Telephony.sms_received ";            @Override public void OnReceive (context context, Intent Intent) {String action = intent.getaction ();            LOG.D (TAG, "action:" +action);                if (Sms_received_action.equals (ACTION)) {Bundle bundle = Intent.getextras (); String messagecontent = bundle.getstring ("message");                Object[] PDUs = (object[]) bundle.get ("PDUs");//Get SMS array smsmessage[] messages = new Smsmessa                Ge[pdus.length];                String format = Intent.getstringextra ("format"); for (int i = 0; i < messages.length; i++) {if (Build.VERSION.SDK_INT >= build.version_codes.                    M) {Messages[i] = SMSMESSAGE.CREATEFROMPDU ((byte[]) pdus[i],format);//convert PDU character array to Smsmessage object } else {Messages[i] = SMSMESSAGE.CREATEFROMPDU ((byte[]) pdus[i])                    ; }} String address = Messages[0].getoriginatingaddress ();//Gets the number of the sender of the SMS string                Fullmessage = "";                for (Smsmessage message:messages) {fullmessage + = Message.getmessagebody ();//Get SMS Content                }//sender.settext (address);                Content.settext (Fullmessage); LOG.D (TAG, "SMS NOTEs: "+fullmessage);            Abortbroadcast (); }        }    }

  

Android SMS send and receive (ii)

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.