How to send text messages

Source: Internet
Author: User
Document directory
  • Call Method
  • How to send a text message to the simulator

There are two ways to send text messages,One is to useIntent-startactivity. The URI data format is "smsto: num". The called action isIntent.Action_sendto:

Uri uri = Uri.Parse("Smsto: 5554 ");

Intent it =
NewIntent (intent.Action_sendto, Uri );

It. putextra ("sms_body ",
"Hello .. ");

Startactivity (it );

 

 

The second is to use smsmanager:

Edittext num = (edittext) findviewbyid (R. Id.Num);

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

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

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

// Obtain smsmanager

Smsmanager SMS = smsmanager.Getdefault();

// If the content is greater than 70 characters, multiple entries are split.

List <string> texts = SMS. dividemessage (smstext );

// Send text messages one by one

For(String text: texts)

{

SMS. sendtextmessage (mobile,
Null, Text,
Null,
Null);

}

// Sending result prompt

Toast.Maketext(Sendsms.This,
"Sent successfully", toast.Length_long). Show ();

The difference between the two is that the former only calls the sending interface and needs to press the send button to send the message, while the latter directly sends the message.

Set the send SMS permission:

<Uses-Permission
Android: Name ="Android. Permission. send_sms"/>

About smsmanager

SDK Introduction: Manages SMS operations such as sending data, text, and pdu sms messages. Get this object by calling
The static method smsmanager. getdefault ().

Method:

Public void sendtextmessage (string destinationaddress, string scaddress, string text, pendingintent sentintent, pendingintent deliveryintent)

 

Destinationaddress: Recipient address

Scaddress: SMS center number. null is the default center number.

Sentintent: when a message is sent, the success or failure information report is broadcast through pendingintent. If this parameter is blank, the sender will be checked by all the location programs, which will prolong the sending time.

Deliveryintent: when a message is sent to the recipient, the pendingintent will be broadcast. PDU data is in extended data ("PDU") of the status report.

If the recipient or information is null, illegalargumentexception is thrown.

Public arraylist <string> dividemessage (string text)

Separate text messages with more than 70 characters into multiple messages.

Parameter: text the original message. Must not be null.

Return: An arraylist of strings that, in order, comprise the original message

The senddatamessage parameter is similar to the preceding one. It is only used to send data.

Sendmultiparttextmessage sends multiple messages. The sent content must be separated by dividemessage.

 

Call Method

 

The call method is similar. The URI format is "Tel: num", and the called action is intent.Action_call:

Edittext edit = (edittext) findviewbyid (R. Id.Dialedit);

String num = edit. gettext (). tostring ();

If(Num! =Null)&&(! "". Equals (Num. Trim ())))

{

Intent intent =NewIntent (intent.Action_call, Uri.Parse("Tel:" + num ));

Startactivity (intent );

}

Call permission settings:

<Uses-Permission
Android: Name ="Android. Permission. call_phone"/>

 

 

 

How to send a text message to the simulator

 

1. Start Android emulator and check the title bar to find the port. Generally, it is Android emulator (5554), of which 5554 is the port.

 

2. Open the command line and enter Telnet localhost 5554. The program will connect to the android console and return

Android Console: Type 'help' for a list of commands

OK

Simulate call to GSM <CALL | accept | busy | cancel | data | hold | list | voice | status>

 

Enter the GSM call <analog phone number>. For example:

GSM invocation 15555218135

Send SMS messages by simulating <senderphonenumber> <textmessage>

Enter SMS send <phone number for sending a simulated text message> <content>. For example:

SMS send 15555218135 hello

15555218135 indicates the phone number of the simulator.

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.