How to send SMS
There are two ways to send text messages, one is to use the intent-startactivity,uri data format of "Smsto:num", the call ACTION is intent.action_sendto :
Uri uri = uri.parse ("smsto:5554"new Intent (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 (); //Get SmsmanagerSmsmanager SMS=Smsmanager.getdefault (); //if the content is greater than 70 words, split into multipleList<String> texts=sms.dividemessage (Smstext); //send a text message for(String text:texts) {sms.sendtextmessage (Mobile,NULL, text,NULL,NULL); } //Send result hintToast.maketext (sendsms. This, "Send Success", Toast.length_long). Show ();
The difference is that the former just call the sending interface, you need to press the Send button text message to send out, and the latter is sent directly out.
Settings for sending SMS permissions:
<uses-permission android:name= "Android.permission.SEND_SMS"/>
About Smsmanager
Description in the SDK: 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, Pendingi Ntent deliveryintent)
Destinationaddress: Recipient Address
Scaddress: SMS Center number, NULL for default center number
Sentintent: When a message is sent, a successful or failed information report is broadcast through Pendingintent. If the parameter is empty, the sender is checked by all location programs again, which results in longer send times.
Deliveryintent: When a message is sent to the recipient, the Pendingintent is broadcast. The PDU data is in Extended data ("PDU") of the status report.
If the recipient or information is empty, IllegalArgumentException is thrown.
Public arraylist<string> dividemessage (String text)
Divide text messages that are larger than 70 words into multiple lines.
Parameter: text the original message. Must not is null.
Returns: An ArrayList of strings, in order, comprise the original message
The Senddatamessage parameter is similar to the above, just for sending data.
Sendmultiparttextmessage send more than one text message, send content must be separated by Dividemessage.
How to call
The method of calling is similar, what is not used is the URI format "Tel:num", and the call 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=new Intent (Intent.action_call,uri.parse ("Tel:" +num)); StartActivity (intent);}
Settings for Call permissions:
<uses-permission android:name= "Android.permission.SEND_SMS"/>
How to send SMS to the simulator
1. Start Android Emulator and look at the title bar to find the port. Typically Android emulator (5554), where 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
Analog phone into GSM <call|accept|busy|cancel|data|hold|list|voice|status>
Enter the phone number > for GSM call < analog. Such as:
GSM Call 15555218135
Analog SMS Sending SMS send <senderPhoneNumber> <textmessage>
Enter SMS send < analog to send SMS phone > < content >. Such as:
SMS Send 15555218135 Hello
Of these, 15555218135 is the simulator mobile number.