Android Study Notes 3 -------- text message sender

Source: Internet
Author: User

:


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 SendMessageActivity extends Activity
{
Private EditText mobileEditText;
Private EditText contenText;
Private Button sendbButton;
Private Button exitButton;

/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
MobileEditText = (EditText) findViewById (R. id. mobile );
ContenText = (EditText) findViewById (R. id. content );
SendbButton = (Button) findViewById (R. id. send );
ExitButton = (Button) findViewById (R. id. exit );

SendbButton. setOnClickListener (listener );
ExitButton. setOnClickListener (listener );
}

Private OnClickListener listener = new OnClickListener ()
{

@ Override
Public void onClick (View v)
{
Button button = (Button) v;
Switch (button. getId ())
{
Case R. id. send:
String mobile = mobileEditText. getText (). toString ();
String contentString = contenText. getText (). toString ();
SmsManager smsManager = SmsManager. getDefault ();

// Intent intent = new Intent ();
// Intent. setAction (Intent. ACTION_SENDTO );
// Intent. setData (Uri. parse ("smsto:" + mobile ));
If (contentString. length ()> 70)
{
List <String> contents = smsManager. divideMessage (contentString );
For (String sms: contents)
{
SmsManager. sendTextMessage (mobile, null, sms, null, null );
// Intent. putExtra ("sms_body", sms );
// SendMessageActivity. this. startActivity (intent );
}
}
Else
{
SmsManager. sendTextMessage (mobile, null, contentString, null, null );
// Intent. putExtra ("sms_body", contentString );
// SendMessageActivity. this. startActivity (intent );
}
Toast. makeText (SendMessageActivity. this, "sms sent", Toast. LENGTH_SHORT). show ();
Break;

Case R. id. exit:
Android. OS. Process. killProcess (android. OS. Process. myPid ());
Break;

Default:
Break;
}

}
};
}
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 SendMessageActivity extends Activity
{
Private EditText mobileEditText;
Private EditText contenText;
Private Button sendbButton;
Private Button exitButton;
 
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
MobileEditText = (EditText) findViewById (R. id. mobile );
ContenText = (EditText) findViewById (R. id. content );
SendbButton = (Button) findViewById (R. id. send );
ExitButton = (Button) findViewById (R. id. exit );

SendbButton. setOnClickListener (listener );
ExitButton. setOnClickListener (listener );
}
 
Private OnClickListener listener = new OnClickListener ()
{

@ Override
Public void onClick (View v)
{
Button button = (Button) v;
Switch (button. getId ())
{
Case R. id. send:
String mobile = mobileEditText. getText (). toString ();
String contentString = contenText. getText (). toString ();
SmsManager smsManager = SmsManager. getDefault ();

// Intent intent = new Intent ();
// Intent. setAction (Intent. ACTION_SENDTO );
// Intent. setData (Uri. parse ("smsto:" + mobile ));
If (contentString. length ()> 70)
{
List <String> contents = smsManager. divideMessage (contentString );
For (String sms: contents)
{
SmsManager. sendTextMessage (mobile, null, sms, null, null );
// Intent. putExtra ("sms_body", sms );
// SendMessageActivity. this. startActivity (intent );
}
}
Else
{
SmsManager. sendTextMessage (mobile, null, contentString, null, null );
// Intent. putExtra ("sms_body", contentString );
// SendMessageActivity. this. startActivity (intent );
}
Toast. makeText (SendMessageActivity. this, "sms sent", Toast. LENGTH_SHORT). show ();
Break;

Case R. id. exit:
Android. OS. Process. killProcess (android. OS. Process. myPid ());
Break;
 
Default:
Break;
}

}
};
}


Main. xml file

 

[Java]

<? 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"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Enter your mobile phone number"
/>

<EditText
Android: layout_height = "wrap_content"
Android: id = "@ + id/mobile"
Android: layout_width = "match_parent"
> </EditText>

<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Enter text message content"
> </TextView>

<EditText
Android: layout_height = "wrap_content"
Android: layout_width = "match_parent"
Android: id = "@ + id/content"
Android: lines = "3"
> </EditText>

<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: id = "@ + id/send"
Android: text = "send SMS"
> </Button>

<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: id = "@ + id/exit"
Android: text = "Exit software"
> </Button>
</LinearLayout>
<? 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"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Enter your mobile phone number"
/>

<EditText
Android: layout_height = "wrap_content"
Android: id = "@ + id/mobile"
Android: layout_width = "match_parent"
> </EditText>
 
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Enter text message content"
> </TextView>
 
<EditText
Android: layout_height = "wrap_content"
Android: layout_width = "match_parent"
Android: id = "@ + id/content"
Android: lines = "3"
> </EditText>
 
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: id = "@ + id/send"
Android: text = "send SMS"
> </Button>
 
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: id = "@ + id/exit"
Android: text = "Exit software www.2cto.com"
> </Button>
</LinearLayout>
 
 
 
 
Add permission:
<Uses-permission android: name = "android. permission. SEND_SMS"> </uses-permission>
 
From running snails
 

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.