Basic Android tutorial-SMS simple text message Program (communication between two simulators )!

Source: Internet
Author: User

The preceding example demonstrates how to call a phone through a program.GSMIn the mobile communication system, apart from making phone calls, another common function is to send text messages. because of this, many telecom carriers have released special rates for the exclusive SMS family, so we can see the importance of the SMS function to mobile phones.

 

The key process for sending text messages is throughSmsmanagerObjectSendtextmessage ()Method.Sendtextmessage ()The method requires five values, followed by the recipient address (String), Sending Address (String), Sending Service (Pendingintent) And Delivery Service (Pendingintent), The recipient and body cannot beNull.

 

This example uses two simulators,5554,5556Communication. Next I will take five steps to explain how the SMS sending program is implemented.

 

Step 1: CreateAndroidProject, we name itSmsdemo.

 

Step 2: Design the programUI, That is, the main interfaceMain. xml, Used hereAbsolutelayoutA little ugly! The Code is as follows:

 

<? XML version = "1.0" encoding = "UTF-8"?>
<Absolutelayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Xmlns: Android = "http://schemas.android.com/apk/res/android"
>
<Textview
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Recipient :"
Android: textsize = "16sp"
Android: layout_x = "0px"
Android: layout_y = "12px"
>
</Textview>
<Edittext
Android: Id = "@ + ID/myedittext1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = ""
Android: textsize = "18sp"
Android: layout_x = "60px"
Android: layout_y = "2px"
>
</Edittext>
<Edittext
Android: Id = "@ + ID/myedittext2"
Android: layout_width = "fill_parent"
Android: layout_height = "223px"
Android: text = ""
Android: textsize = "18sp"
Android: layout_x = "0px"
Android: layout_y = "52px"
>
</Edittext>
<Button
Android: Id = "@ + ID/mybutton1"
Android: layout_width = "162px"
Android: layout_height = "wrap_content"
Android: text = "send SMS"
Android: layout_x = "80px"
Android: layout_y = "302px"
>
</Button>
</Absolutelayout>

Step 3:Main Control ProgramSmsdemo. JavaAs follows:

Package com. Android. test;

Import Android. App. activity;
Import Android. App. pendingintent;
Import Android. content. intent;

Import Android. OS. Bundle;
Import Android. telephony. GSM. smsmanager;
Import Android. View. view;
Import Android. widget. Button;
Import Android. widget. edittext;
Import Android. widget. Toast;

Public class smsdemo extends activity {

 Private button mbutton1;
Private edittext medittext1;
Private edittext medittext2;
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

// Obtain resources
Medittext1 = (edittext) findviewbyid (R. Id. myedittext1 );
Medittext2 = (edittext) findviewbyid (R. Id. myedittext2 );
Mbutton1 = (button) findviewbyid (R. Id. mybutton1 );
// Send SMS response
Mbutton1.setonclicklistener (New button. onclicklistener (){

Public void onclick (view v ){
// Obtain the Sending address and content
String messageaddress = medittext1.gettext (). tostring ();
String messagecontent = medittext2.gettext (). tostring ();

// Construct an smsmanager object for obtaining the default instance

Smsmanager = smsmanager. getdefault ();
// Check whether the entered content is empty. For the sake of simplicity, the text message content length limit is not set.
If (messageaddress. Trim (). Length ()! = 0 & messagecontent. Trim (). Length ()! = 0)
{
Try {
Pendingintent pintent = pendingintent. getbroadcast (smsdemo. This, 0, new intent (), 0 );
Smsmanager. sendtextmessage (messageaddress, null, messagecontent, pintent, null );

} Catch (exception E)
{
E. printstacktrace ();
}
// Prompt that the message is sent successfully
Toast. maketext (smsdemo. This, "sent successfully", Toast. length_long). Show ();
}
Else {
Toast. maketext (smsdemo. This, "The Sending address or content cannot be blank", Toast. length_short). Show ();
}
}

});
}
}

 

Step 4:Add call permissionAndroidmanifest. xmlThe Code is as follows:

 

<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. Android. test"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">
<Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
<Activity Android: Name = ". smsdemo"
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-SDK Android: minsdkversion = "3"/>
<Uses-Permission Android: Name = "android. Permission. send_sms"> </uses-Permission>
</Manifest>

 

Step 5: Run it!As follows,5554To5556Sent a text message:

 

 

 

OK ~ This is the end of today. Thank you for your attention!

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.