Android SMS/MMS

Source: Internet
Author: User

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 = SmsManager. getDefault ();
// Intent object
PendingIntent 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 = SmsManager. getDefault ();

// Intent object
PendingIntent 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 menu ){
// Inflate the menu; this adds items to the action bar if it is present.
GetMenuInflater (). inflate (R. menu. sms, menu );
Return true;
}

}

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.