Sample code for sending text messages using Android

Source: Internet
Author: User
This article mainly introduces the example of a small program for sending text messages in Android development. It also includes an upgraded text message sending example for listening to the broadcast receiver, for more information about how to send text messages in Android development, see this article. This article also provides an upgraded text message sending example for listening to broadcast receivers, for more information, see

Package cn.com. sms. send; import java. util. arrayList; import java. util. iterator; import android. app. activity; import android. app. pendingIntent; import android. content. intent; import android. OS. bundle; import android. telephony. smsManager; import android. util. log; import android. view. view; import android. widget. button; import android. widget. editText; import android. widget. toast; public class Send exte Nds Activity {private String message; private String number; private EditText editText; private EditText editText2; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); editText = (EditText) this. findViewById (R. id. number); editText2 = (EditText) this. findViewById (R. id. message); Button button = (Button) this. findViewById (R. id. Button); button. setOnClickListener (new View. onClickListener () {public void onClick (View v) {number = editText. getText (). toString (); message = editText2.getText (). toString (); // You can view the related information Log of number and message in LogCat. I ("number", number); Log. I ("message", message);/* obtain the default information manager of the system. Note that SmsManager is android. telephony. smsManager; this is related to * the version we used. Android should be used before android 2.0. telephony. gsm. smsManager * And Android. telephony. SmsManager should be used for versions later than roid 2.0. */SmsManager smsManager = SmsManager. getDefault ();/* PendingIntent. getBroadcast returns a PendingIntent object for broadcasting, similar to calling Content. sendBroadcast (); */PendingIntent paIntent = PendingIntent. getBroadcast (Send. this, 0, new Intent ("SMS_SENT"), 0); PendingIntent deliveryIntent = PendingIntent. getBroadcast (Send. this, 0, new Intent ("SMS_DELIVERED"), 0); // smsManager. pideMessage sometimes if the number of text messages exceeds the limit, we need this method to split the text message content. ArrayList
 
  
Smses = smsManager. pideMessage (message); Iterator
  
   
Iterator = smses. iterator (); while (iterator. hasNext () {String temp = iterator. next (); // send SMS smsManager. sendTextMessage (number, null, temp, paIntent, deliveryIntent);} // a floating box is displayed, showing the prompt content, Toast. LENGTH_LONG indicates the length of time Toast is displayed in the floating box. makeText (Send. this, "message sent successfully", Toast. LENGTH_LONG ). show ();}});}}
  
 

Main. xml


  
  
    
    
    
    
   
 

AndroidManifest. xml


  
    
     
                  
                    
          
            
 

Eventually:

SmsManager smsMgr = SmsManager.getDefault();  smsMgr.sendTextMessage(address, null, message, null, null);


The text message writing interface is displayed:


Uri smsToUri = Uri.parse("smsto://10086");  Intent mIntent = new Intent( android.content.Intent.ACTION_SENDTO, smsToUri );  startActivity( mIntent );


Send email:


Intent i = new Intent(Intent.ACTION_SEND);  i.putExtra(Intent.EXTRA_EMAIL, address);  i.putExtra(Intent.EXTRA_SUBJECT, filename);  i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + filename)); ;  i.setType("text/csv");  startActivity(Intent.createChooser(i, "EMail File"));

Upgrade:
This code adds a listener for the broadcast receiver. The code is as follows:


Package cn.com. sms. send; import java. util. arrayList; import java. util. iterator; import android. app. activity; import android. app. pendingIntent; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. content. intentFilter; import android. OS. bundle; import android. telephony. smsManager; import android. util. log; import android. view. view; im Port android. widget. button; import android. widget. editText; import android. widget. toast; public class Send extends Activity {private String message; private String number; private EditText editText; private EditText editText2; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); editText = (EditText) this. findViewById (R. id. Number); editText2 = (EditText) this. findViewById (R. id. message); Button button = (Button) this. findViewById (R. id. button); button. setOnClickListener (new View. onClickListener () {public void onClick (View v) {number = editText. getText (). toString (); message = editText2.getText (). toString (); // You can view the related information Log of number and message in LogCat. I ("number", number); Log. I ("message", message);/* get the default information manager of the system. Note that Sm SManager is android. telephony. smsManager; this is related to * the version we used. Android should be used before android 2.0. telephony. gsm. smsManager * Android 2.0 and later versions should use android. telephony. smsManager. */SmsManager smsManager = SmsManager. getDefault ();/* PendingIntent. getBroadcast returns a PendingIntent object for broadcasting, similar to calling Content. sendBroadcast (); */PendingIntent paIntent = PendingIntent. getBroadcast (Send. this, 0, new Intent ("SMS_SENT2"), 0); PendingIntent deliveryIntent = PendingIntent. getBroadcast (Send. this, 0, new Intent ("SMS_DELIVERED2"), 0); // register a BroadcastReceiver. this method is triggered when an Intent that matches its IntentFilter appears. RegisterReceiver (new BroadcastReceiver () {@ Override public void onReceive (Context context, Intent intent) {int resultCode = getResultCode (); switch (resultCode) {case Activity. RESULT_ OK: Toast. makeText (getBaseContext (), "message sent successfully.", Toast. LENGTH_LONG ). show (); break; default: Toast. makeText (getBaseContext (), "message sending failed.", Toast. LENGTH_LONG ). show () ;}}, new IntentFilter ("SMS_SENT2"); registerRec Eiver (new BroadcastReceiver () {@ Override public void onReceive (Context context, Intent intent) {Toast. makeText (getBaseContext (), "deliveryIntent", Toast. LENGTH_LONG ). show (); Log. I ("check message recipient", "check") ;}, new IntentFilter ("SMS_DELIVERED2"); // smsManager. pideMessage sometimes if the number of text messages exceeds the limit, we need this method to split the text message content. ArrayList
 
  
Smses = smsManager. pideMessage (message); Iterator
  
   
Iterator = smses. iterator (); while (iterator. hasNext () {String temp = iterator. next (); // send SMS smsManager. sendTextMessage (number, null, temp, paIntent, deliveryIntent);} // a floating box is displayed, showing the prompt content, Toast. LENGTH_LONG indicates the length of time Toast is displayed in the floating box. makeText (Send. this, "message sent successfully", Toast. LENGTH_LONG ). show ();}});}}
  
 

Main. xml and AndroidManifest. xml are the same as the previous code.

RegisterReceiver () is used to register a broadcast receiver. This method is defined in Content as follows.

Public abstract Intent registerReceiver (BroadcastReceiver receiver, IntentFilter filter); if the system queries broadcasts that meet the filter conditions, it will teach the receiver to process them. It is generally processed in its onReceive () method.

If you do not actively register using registerReceiver () in the code, you must configure it from AndroidManifest. xml. the code is as follows:


  
    
   
 

Note that in the configuration file, the activity tag and the worker tag are of the same level.

If you send Chinese characters in the simulator, garbled characters will occur on the receiver, but no garbled characters will occur on the real machine. Therefore, developers only need to normally develop the text message function, and do not need to convert the code.

The above is the detailed content of the sample code of the mini-program that Android uses to send text messages. For more information, see other related articles in the first PHP community!

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.