Android sends SMS messages

Source: Internet
Author: User

To use the SMS service, you must add the SMS Service permission to AndroidManifest. xml.
AndroidManifest. xml
<Uses-permission android: name = "android. permission. SEND_SMS"/> <! -- Add permissions -->
 
Package com. sms;
Import java. util. List;
Import android. app. Activity;
Import android. app. PendingIntent;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. telephony. SmsManager;
Import android. view. View;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. Toast;
Public class MainActivity extends Activity implements
Android. view. View. OnClickListener {
/** Called when the activity is first created .*/
Private EditText contact;
Private EditText smsContent;
Private Button commit;
Private String strNo;
Private String strSms;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
InitView ();
OnClickListener ();
}
Public void initView (){
Contact = (EditText) findViewById (R. id. contact );
SmsContent = (EditText) findViewById (R. id. smsContent );
Commit = (Button) findViewById (R. id. commit );
}
Public void onClickListener (){
Commit. setOnClickListener (this );
}
@ Override
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. commit:
StrNo = contact. getText (). toString ();
StrSms = smsContent. getText (). toString ();
// TODO Auto-generated method stub
/*
* Android. telephony. gsm. SmsManager should be used before android 2.0.
* Android. telephony. SmsManager;
*/
// Obtain the default SMS manager of the system.
SmsManager smsManager = SmsManager. getDefault ();
PendingIntent sentIntent = PendingIntent. getBroadcast (
MainActivity. this, 0, new Intent (), 0 );
// If the number of words exceeds 70, you need to split the text message into multiple messages for sending.
// Split the text message according to the maximum number of characters in each text message
If (strSms. length ()> 70 ){
List <String> msgs = smsManager. divideMessage (strSms );
For (String msg: msgs ){
/*
* Send SMS
*
* SmsManager. sendTextMessage (destinationAddress, scAddress,
* Text, sentIntent, deliveryIntent)
*
* -- DestinationAddress: The target phone number.
*
* -- ScAddress: the number of the SMS center. This parameter can be left blank during the test.
*
* -- Text: text message content
*
* -- SentIntent: Send --> China Mobile failed to send --> return a message indicating successful or failed sending -->
* Subsequent processing means that this intent encapsulates the message sending status information.
*
* -- DeliveryIntent: Send --> China Mobile sent successfully -->
* Whether the other party receives the message --> subsequent processing
* That is, this intent encapsulates the status information received by the other party (the supplier has successfully sent the message but the other party has not received it ).
*/
SmsManager. sendTextMessage (strNo, null, msg, sentIntent,
Null );
}
} Else {
SmsManager. sendTextMessage (strNo, null, strSms, sentIntent,
Null );
}
Toast. makeText (MainActivity. this, "message sent successfully", Toast. LENGTH_LONG)
. Show ();
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.