Android [call, send and receive text messages]

Source: Internet
Author: User

1. call;

Edittext mobiletext = (edittext) findviewbyid (R. Id. Mobile );

String mobile = mobiletext. gettext (). tostring ();

Intent intent = new intent ();

Intent. setaction ("android. Intent. Action. Call ");

Intent. setdata (URI. parse ("Tel:" + mobile ));

Startactivity (intent );

 

2. send text messages

Edittext mobiletext = (edittext) findviewbyid (R. Id. Mobile );

Edittext contenttext = (edittext) findviewbyid (R. Id. content );

String mobile = mobiletext. gettext (). tostring ();

String content = contenttext. gettext (). tostring ();

Smsmanager = smsmanager. getdefault ();

Arraylist <string> texts = smsmanager. dividemessage (content); // split the SMS

For (string text: texts ){

Smsmanager. sendtextmessage (mobile, null, text, null, null );

}

You must declare in androidmanifest: <uses-Permission Android: Name = "android. Permission. send_sms"/>

 

3. receive SMS:

Public class smsreceiver extends broadcastreceiver

{

 

@ Override

Public void onreceive (context, intent)

{

 

If ("android. provider. telephony. sms_received"

. Equals (intent. getaction ()))

{

String phonenumber = NULL;

String message = NULL;

// Receives data transmitted by SMS.

Bundle bundle = intent. getextras ();

// Determine whether data exists

If (bundle! = NULL)

{

// All received SMS messages can be obtained through PDUS.

Object [] objarray = (object []) bundle. Get ("PDUS ");

/* Construct the SMS object array and create the array size based on the length of the received object */

Smsmessage [] messages = new smsmessage [objarray. Length];

For (INT I = 0; I <objarray. length; I ++)

{

Messages [I] = smsmessage

. Createfrompdu (byte []) objarray [I]);

}

 

For (smsmessage currentmessage: messages)

{

Phonenumber = currentmessage. getdisplayoriginatingaddress ();

Message = currentmessage. getdisplaymessagebody ();

}

}

Toast. maketext (context, "Number =" + phonenumber + "MSG =" + message, Toast. length_long). Show ();

}

}

 

} You must declare in androidmanifest: <uses-Permission Android: Name = "android. Permission. receive_sms"/>

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.