Android SMS sending implementation tips

Source: Internet
Author: User

Programmers can make some changes to the Android mobile operating system to meet users' needs. However, to modify the source code, you must first understand how to write the source code. Here we will first take a look at the specific implementation of the Android text message function to experience the relevant writing methods.

1: Android SMS sending can be simulated in the simulator.

For example, if the id of the first simulator is 5554, run cmd.
Telnet local host 5554

Enter help to see many functional commands used in simulators.

 
 
  1. Gsm invocation 134343434
  2. // Call the current simulator command
  3. Sms send 15555218135 Hello, this is a Message
  4. // Send a short message to the current Simulator

2: related classes:

 
 
  1. Android.telephony.gsm.SmsManager   
  2. Android.telephony.gsm.SmsMessage   
  3. Android.app.PendingIntent   
  4. Android.widget.Toast  

3: Android text message sending implementation code excerpt)

 
 
  1. String msg ="hello";   
  2. string number = "1234565678";   
  3. SmsManager sms = SmsManager.getDefault();   
  4. PendingIntent pi = PendingIntent.
    getBroadcast(Sms.this,0,new Intent(),0);   
  5. sms.sendTextMessage(number,null,msg,pi,null);   
  6. Toast.makeText(Sms.this,"send success",
    Toast.LENGHT_LONG).show();  

4: Android text message sending code explanation

The above text message sending code is very simple, but several of the class functions are not easy to understand:

  • Introduction to Android gravity sensing implementation
  • Android Theme details
  • Android Application tips
  • Implementation of Network Image Display in Android
  • How to enable auto-start on Android

Toast. makeText is a prompt, which is easy to understand;

PendingIntent is an Intent description. We can give this description to another program.

According to this description, do what you want to do at the next time. By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other
Application was yourself is equivalent to your representative. In this example, other programs send text messages. After the messages are sent successfully, intent should be broadcast.

Function sendTextMessage (String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)

The first three parameters are easy to understand according to the document. When a PendingIntent sentIntent message is sent successfully, sendIntent broadcasts the intent described internally. Otherwise, an error code is generated and passed to Android. app. pendingIntent. onFinished callback. It is best not to leave this parameter blank, otherwise there may be potential problems of resource waste;

DeliveryIntent is the PendingIntent broadcast after the message has been delivered to the recipient.

View the PendingIntent class. You can see many Send functions, that is, PendingIntent is performing related operations.

The implementation method of Android text message sending is described here.

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.