Android-Basic Knowledge: smsManager. sendTextMessage () sends Short Messages

Source: Internet
Author: User

Use SmsManager to send information. smsManager is a default instance of SmsManager. SmsManager smsManager = SmsManager. getDefault ();

SmsManager. sendTextMessage (destinationAddress, scAddress, text, sentIntent, deliveryIntent)

DestinationAddress: Recipient number

ScAddress: the service number of the SMS center. Set it to null.

Text: Sent content

SentIntent: sends the message Result Status Signal (whether the message is sent successfully). A new Intent is sent. The Intent will be broadcast after the operating system receives the signal. This process is asynchronous.

DeliveryIntent: Status Signal received by the recipient (whether received successfully ).

To use the system message sending function, add <uses-permission android: name = "android. permisson. SEND_SMS"/> to AndroidMainfest. xml.

  

AndroidMainfest. xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.PhoneSMS.melody"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10"/>

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".PhoneSMSActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

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

 

1. Interface Design res/layout/main. xml

 

Main. xml

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<! -- Edit the receiving number -->
<TextView android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" android: text = "@ string/textSMSTo"/>
<EditText android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" android: lines = "3" android: hint = "@ string/edtSMSTo"
Android: id = "@ + id/edtSMSTo"/>
<! -- Edit the sent content -->
<TextView android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" android: text = "@ string/textContent"/>
<EditText android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" android: lines = "3"
Android: hint = "@ string/edtContent" android: id = "@ + id/edtContent"/>
<! -- Send -->
<Button android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: text = "@ string/btnSent"
Android: id = "@ + id/btnSent"/>

</LinearLayout>

 

2. Character variable res/values/strings. xml

 

Strings. xml

<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello World, PhoneSMSActivity! </String>
<String name = "app_name"> PhoneSMS </string>
<String name = "textSMSTo"> recipient </string>
<String name = "edtSMSTo"> recipient number </string>
<String name = "textContent"> sent content </string>
<String name = "edtContent"> input content </string>
<String name = "btnSent"> send </string>
</Resources>

3. Write Activity code

  

PhoneSMSActivity. java

Package com. PhoneSMS. melody;

Import java. util. List;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. telephony. SmsManager;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. Toast;

Public class PhoneSMSActivity extends Activity implements OnClickListener {
Private EditText edtSMSTo = null; // recipient Control
Private EditText edtContent = null; // send content control
Private Button btnSent = null; // send the btn Control
Private String SMSTo = null; // recipient number
Private String SMSContent = null; // sent content


/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

/**
* Get controls
*/
EdtSMSTo = (EditText) this. findViewById (R. id. edtSMSTo );
EdtContent = (EditText) this. findViewById (R. id. edtContent );
BtnSent = (Button) this. findViewById (R. id. btnSent );

// Obtain the recipient's number
SMSTo = edtSMSTo. getText (). toString ();
// Obtain the sent content
SMSContent = edtContent. getText (). toString ();
// Set listener events
BtnSent. setOnClickListener (this );
}

@ Override
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. btnSent:
/**
* SMS sending Function
*/
// Get the default SMS management object
SmsManager smsManager = SmsManager. getDefault ();
// Determine the number of words in the sent content (up to 70 words for a piece of information)
If (SMSContent. length () <= 70 ){
SmsManager. sendTextMessage (SMSTo, null, SMSContent, null, null );
} Else {
// DivideMessage in the SmsManger class splits the information by 70 words
List <String> smsDivs = smsManager. divideMessage (SMSContent );
For (String sms: smsDivs ){
SmsManager. sendTextMessage (SMSTo, null, sms, null, null );

}
}
Toast. makeText (PhoneSMSActivity. this, "message sent", Toast. LENGTH_SHORT );
Break;
}
}
}

 

  

  

  

 

  

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.