Android---send SMS---

Source: Internet
Author: User

Smsmanager: Manage SMS Operations

Gets this object by calling the static method Smsmanager.getdefault ().


Smsmanager


Method Summary
Boolean Copymessagetosim (byte[] smsc, byte[] PDU, int status)
Copy a raw SMS PDU to the SIM.
Boolean deletemessagefromsim (int messageindex)
Delete the specified message from the SIM.
Arraylist<string> dividemessage (String text)
Divide a text message into several messages, none bigger than the maximum SMS message size.
Arraylist<smsmessage> Getallmessagesfromsim ()
Retrieves all messages currently stored on SIM.
Static Smsmanager Getdefault ()
Get the default instance of the Smsmanager
void Senddatamessage (String destinationaddress, String scaddress, short destinationport, byte[] data, pendingintent


Sentintent, Pendingintent deliveryintent)
Send a data based SMS to a specific application port.
void Sendmultiparttextmessage (String destinationaddress, String scaddress, arraylist<string> parts,


Arraylist<pendingintent> sentintents, arraylist<pendingintent> deliveryintents)
Send a multi-part text based SMS.
void Sendtextmessage (String destinationaddress, String scaddress, string text, Pendingintent sentintent, pendingintent


Deliveryintent)
Send a text based SMS.
Boolean Updatemessageonsim (int messageindex, int newstatus, byte[] PDU)
Update the specified message on the SIM.




Manager.sendtextmessage (destinationaddress, scaddress, text, sentintent, deliveryintent);

Destination address of the destinationaddress--message
scaddress--the address of the service center or null use the current default of SMSC 3)
text--the body of the message, the data to be sent by the message
sentintent--if not empty, the pendingintent is broadcast when the message is successfully sent or failed. The result code is ACTIVITY.RESULT_OK indicates success


, or one of Result_error_generic_failure, Result_error_radio_off, RESULT_ERROR_NULL_PDU represents an error. Corresponding


Result_error_generic_failure, Sentintent may include an additional "error code" that contains a radio broadcast technology-specific value, usually only in repairing the fault


is useful.
Each SMS-based application controls the detection of sentintent. If Sentintent is empty, the caller will detect all unknown applications, which will result in detection


To send a smaller number of SMS.
deliveryintent--If the message is not empty, the pendingintent is broadcast when it is successfully delivered to the receiver.



Mainactivity.java:


Import Java.util.arraylist;import Android.app.activity;import Android.os.bundle;import Android.telephony.gsm.smsmanager;import Android.view.menu;import Android.view.menuitem;import Android.view.View; Import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import android.widget.toast;/** * Send SMS * * @author Caesar * */public class Mainactivity extends Activity implements Onclicklist Ener {private EditText numtext;private EditText contenttext;private button button; @Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); NumText = ( EditText) Findviewbyid (r.id.number); contenttext = (EditText) Findviewbyid (r.id.content); button = (button) Findviewbyid (R.id.button); Button.setonclicklistener (this);} @Overridepublic void OnClick (View v) {//TODO auto-generated method stubstring number = Numtext.gettext (). toString (); String content = Contenttext.gettext (). toString ();//SmsmanagER: Manage SMS operation, get this object by static method Smsmanager.getdefault () Smsmanager manager = Smsmanager.getdefault (); arraylist<string> texts = manager.dividemessage (content); for (String text:texts) {manager.sendtextmessage ( Number, NULL, text, NULL, NULL);} Toast.maketext (Mainactivity.this, "Send Success", Toast.length_long). Show ();}}


Activity_main.xml:


<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "Android:orienta tion= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_ Vertical_margin "tools:context=" com.example.sendmessagedemo.MainActivity "> <textview android:layout_wi Dth= "Fill_parent" android:layout_height= "wrap_content" android:text= "Enter phone number"/> <edittext and Roid:id= "@+id/number" android:layout_width= "fill_parent" android:layout_height= "wrap_content"/> <T  Extview android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "Input SMS Content"   /> <edittext android:id= "@+id/content"     Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:minlines= "3"/> & Lt Button android:id= "@+id/button" android:layout_width= "wrap_content" android:layout_height= "Wrap_conte NT "android:text=" send "/></linearlayout>


You need to add permissions:


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





Android---send 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.