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.
- Gsm invocation 134343434
- // Call the current simulator command
- Sms send 15555218135 Hello, this is a Message
- // Send a short message to the current Simulator
2: related classes:
- Android.telephony.gsm.SmsManager
- Android.telephony.gsm.SmsMessage
- Android.app.PendingIntent
- Android.widget.Toast
3: Android text message sending implementation code excerpt)
- String msg ="hello";
- string number = "1234565678";
- SmsManager sms = SmsManager.getDefault();
- PendingIntent pi = PendingIntent.
getBroadcast(Sms.this,0,new Intent(),0);
- sms.sendTextMessage(number,null,msg,pi,null);
- 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.