Android (4) _ text message operation

Source: Internet
Author: User

Strings. xml

[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
 
<String name = "app_name"> lession01_sms </string>
<String name = "action_settings"> Settings </string>
<String name = "phone_num"> enter the mobile phone number </string>
<String name = "phone_content"> enter the sent content </string>
<String name = "phone_button"> send SMS </string>
 
</Resources>

<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>

<String name = "app_name"> lession01_sms </string>
<String name = "action_settings"> Settings </string>
<String name = "phone_num"> enter the mobile phone number </string>
<String name = "phone_content"> enter the sent content </string>
<String name = "phone_button"> send SMS </string>

</Resources>

 


Layout file: activity_sms.xml

[Html]
<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/view_phone"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/phone_num"/>
<EditText
Android: id = "@ + id/edit_num"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: inputType = "phone"
Android: layout_below = "@ + id/view_phone"
Android: layout_alignParentLeft = "true"/>
<TextView
Android: id = "@ + id/view_content"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/phone_content"
Android: layout_below = "@ + id/edit_num"/>
<EditText
Android: id = "@ + id/edit_content"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: inputType = "text"
Android: minLines = "5"
Android: layout_below = "@ + id/view_content"/>
<Button
Android: id = "@ + id/button_send"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/phone_button"
Android: layout_below = "@ + id/edit_content"/>
 
</RelativeLayout>

<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/view_phone"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/phone_num"/>
<EditText
Android: id = "@ + id/edit_num"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: inputType = "phone"
Android: layout_below = "@ + id/view_phone"
Android: layout_alignParentLeft = "true"/>
<TextView
Android: id = "@ + id/view_content"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/phone_content"
Android: layout_below = "@ + id/edit_num"/>
<EditText
Android: id = "@ + id/edit_content"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: inputType = "text"
Android: minLines = "5"
Android: layout_below = "@ + id/view_content"/>
<Button
Android: id = "@ + id/button_send"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/phone_button"
Android: layout_below = "@ + id/edit_content"/>

</RelativeLayout>
 

 


Add permission

 

 

SmsActivity. java

[Java]
Package com. example. lession01_sms;
 
Import java. util. ArrayList;
 
Import android. OS. Bundle;
Import android. app. Activity;
Import android. app. PendingIntent;
Import android. content. Intent;
Import android. telephony. SmsManager;
Import android. view. Menu;
Import android. view. View;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. Toast;
 
Public class SmsActivity extends Activity {
 
// Declare the control object
Public Button btn_send;
Public EditText edit_num;
Public EditText edit_content;
 
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// Set the displayed view (layout file)
SetContentView (R. layout. activity_sms );
// Obtain the control object
Btn_send = (Button) findViewById (R. id. button_send );
Edit_num = (EditText) findViewById (R. id. edit_num );
Edit_content = (EditText) findViewById (R. id. edit_content );
 
// Register events for button controls
Btn_send.setOnClickListener (new View. OnClickListener (){
 
@ Override
Public void onClick (View arg0 ){
// Obtain the phone number
String phone_num = edit_num.getText (). toString ();
// Obtain the sent content
String phone_content = edit_content.getText (). toString ();
// Obtain the text message manager object
SmsManager smsManager = SmsManager. getDefault ();
// Intent object
PendingIntent pendingIntent = PendingIntent. getBroadcast (
SmsActivity. this, 0, new Intent (), 0 );
 
// 70 pages
ArrayList <String> contents = smsManager. divideMessage (phone_content );
For (String c: contents ){
SmsManager. sendTextMessage (phone_num, null, c, pendingIntent, null );
}


// Send information
SmsManager. sendTextMessage (phone_num, null, phone_content,
PendingIntent, null );
// Toast Effect
Toast. makeText (SmsActivity. this, "sent", 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;
}
 
}

Package com. example. lession01_sms;

Import java. util. ArrayList;

Import android. OS. Bundle;
Import android. app. Activity;
Import android. app. PendingIntent;
Import android. content. Intent;
Import android. telephony. SmsManager;
Import android. view. Menu;
Import android. view. View;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. Toast;

Public class SmsActivity extends Activity {

// Declare the control object
Public Button btn_send;
Public EditText edit_num;
Public EditText edit_content;

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// Set the displayed view (layout file)
SetContentView (R. layout. activity_sms );
// Obtain the control object
Btn_send = (Button) findViewById (R. id. button_send );
Edit_num = (EditText) findViewById (R. id. edit_num );
Edit_content = (EditText) findViewById (R. id. edit_content );

// Register events for button controls
Btn_send.setOnClickListener (new View. OnClickListener (){

@ Override
Public void onClick (View arg0 ){
// Obtain the phone number
String phone_num = edit_num.getText (). toString ();
// Obtain the sent content
String phone_content = edit_content.getText (). toString ();
// Obtain the text message manager object
SmsManager smsManager = SmsManager. getDefault ();
// Intent object
PendingIntent pendingIntent = PendingIntent. getBroadcast (
SmsActivity. this, 0, new Intent (), 0 );

// 70 pages
ArrayList <String> contents = smsManager. divideMessage (phone_content );
For (String c: contents ){
SmsManager. sendTextMessage (phone_num, null, c, pendingIntent, null );
}


// Send information
SmsManager. sendTextMessage (phone_num, null, phone_content,
PendingIntent, null );
// Toast Effect
Toast. makeText (SmsActivity. this, "sent", 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.