First, activity_main.xml layout:
<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Xmlns: Tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
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 = ". smsactivity">
<Textview
Android: Id = "@ + ID/phone_lable"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignparenttop = "true"
Android: layout_margintop = "23dp"
Android: text = "@ string/phon_lable"/>
<Edittext
Android: Id = "@ + ID/edit_phone"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_below = "@ + ID/phone_lable"
Android: layout_centerhorizontal = "true"
Android: EMS = "10"
Android: inputtype = "phone">
</Edittext>
<Textview
Android: Id = "@ + ID/sms_lable"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignleft = "@ + ID/edit_phone"
Android: layout_below = "@ + ID/edit_phone"
Android: layout_margintop = "22dp"
Android: text = "@ string/sms_lable"/>
<Button
Android: Id = "@ + ID/but_sms"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignleft = "@ + ID/edit_sms"
Android: layout_alignparentbottom = "true"
Android: layout_marginbottom = "14dp"
Android: text = "@ string/but_sms"/>
<Edittext
Android: Id = "@ + ID/edit_sms"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_above = "@ + ID/but_sms"
Android: layout_alignleft = "@ + ID/sms_lable"
Android: EMS = "10"
Android: inputtype = "textmultiline"/>
</Relativelayout>
Then, add the SMS sending permission to androidmanifest. xml:
<Uses-Permission Android: Name = "android. Permission. send_sms"/>
The following describes how to implement the main code in smsactivity:
Public class smsactivity extends activity {
// Declare the control object
Private button butten_sms;
Private edittext edit_phone;
Private edittext edit_sms;
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Set the display view
Setcontentview (R. layout. activity_sms );
// Obtain the button component
Butten_sms = (button) findviewbyid (R. Id. but_sms );
// Obtain the input box component
Edit_phone = (edittext) findviewbyid (R. Id. edit_phone );
Edit_sms = (edittext) findviewbyid (R. Id. edit_sms );
// Register button event
Butten_sms.setonclicklistener (New View. onclicklistener (){
@ Override
Public void onclick (view arg0 ){
/**
* Send MMS messages
*/
// Obtain the phone number
String phone_num = edit_phone.gettext (). tostring ();
// Create intent object
Intent intent = new intent (intent. action_send );
Intent. putextra (intent. extra_stream, Uri. parse ("file: // sdcard/a.jpg "));
Intent. putextra ("Address", phone_num );
Intent. putextra ("exit_on_sent", true );
Intent. putextra ("subject", "Subject: I love you ");
Intent. putextra ("sms_body", "content: XXXX ");
Intent. settype ("image/JPEG ");
Startactivity (intent );
/* // Obtain the text message manager object
Smsmanager = smsmanager. getdefault ();
// Intent object
Pendingintent = pendingintent. getbroadcast (
Smsactivity. This, 0, new intent (), 0 );*/
}
});
}
// Text message function
Public void send (){
// Obtain the number
String phone_num = edit_phone.gettext (). tostring ();
// Get the sent text message
String phone_sms = edit_sms.gettext (). tostring ();
// Obtain the information manager object
Smsmanager = smsmanager. getdefault ();
// Intent object
Pendingintent = pendingintent. getbroadcast (smsactivity. This, 0, new intent (), 0 );
// If the message length is greater than 70, two messages are sent .. The default value is 70.
Arraylist <string> contents = smsmanager
. Dividemessage (phone_sms );
For (string C: Contents ){
// Send information
Smsmanager. sendtextmessage (phone_num, null, C,
Pendingintent, null );
}
/* // Send information
Smsmanager. sendtextmessage (phone_num, null, phone_sms, pendingintent, null );*/
// Effect of toast
Toast. maketext (smsactivity. This, "sms sent successfully", Toast. length_long). Show ();
}
@ Override
Public Boolean oncreateoptionsmenu (menu ){
// Inflate the menu; this adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R. Menu. SMS, menu );
Return true;
}
}