Send and receive SMS

Source: Internet
Author: User



Permissions:

<uses-permission android:name= "Android.permission.READ_SMS" ></uses-permission>

< uses-permission android:name= "Android.permission.SEND_SMS" ></uses-permission>


Send SMS:

1. Get the default message manager

2. Send SMS via Sendtextmessage.

Smsmanager sm = Smsmanager.getdefault ();
Sm.sendtextmessage ("1888888XXX", NULL, str, NULL, NULL);

Receive SMS

When a text message is received, an action name called Android.provier.Telephony.SMS_RECEIVED's broadcast intent is sent, and the intent holds the SMS content that is taken over, and the application name "PDUs" Get SMS content from intent

Registerreceiver (New Broadcastreceiver () {

@Override
Public void OnReceive (context context, Intent Intent) {
//TODO auto-generated method stub
Bundle bundle = Intent.getextras ();
smsmessage msgs = null;
 String str = "";
 if (bundle! = null)
 {
//---Retrieve the SMS message received---
object[] PDUs = (object[]) bundle.get ("PDUs");
for (int i=0;i<pdus.length; i++) {
MSGS=SMSMESSAGE.CREATEFROMPDU ((byte[]) pdus[i]);

str + = Msgs.getmessagebody (). toString ();
}
}

}

}, New Intentfilter ("Android.provider.Telephony.SMS_RECEIVED"));

Send and receive integration

Context.registerreceiver (New Broadcastreceiver () {
@Override
Public void OnReceive (context context, Intent Intent) {
//TODO auto-generated method stub
Bundle bundle = Intent.getextras ();
smsmessage msgs = null;
String str = "";
if (bundle! = null)
 {
//---Retrieve the SMS message received---
  object[] PDUs = (object[]) bundle.get ("PDUs");
for (int i=0; I<pdus.length; i++ {
msgs=sMSMESSAGE.CREATEFROMPDU ((byte[]) pdus[i]);
str + = Msgs.getmessagebody (). toString ();

}

else {//forwarding SMS
Smsmanager sm = Smsmanager.getdefault ();
sm.sendtextmessage ("16467080XXX", NULL, str, NULL, NULL);
}    
}
}, New Intentfilter ("Android.provider.Telephony.SMS_RECEIVED"));


Send and receive SMS

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.