How does Android receive and send SMS

Each mobile phone has the function of receiving and sending SMS. Below, we realize the function of receiving and sending SMS through code.

1、 Receive SMS

1. Create an internal broadcast receiver class to receive the SMS broadcast sent by the system. 2. Parse the SMS sender and SMS content from the obtained content. 3. Register the broadcast in activity. 4. Add the permission to receive SMS

Put the specific code activity below_ The main.xml file is used to display the SMS sender number and display the SMS content

Mainactivity.java file

Note: the registered broadcast receiver must be unregistered in ondestroy() method.

Since the SMS broadcast is an orderly broadcast, if we don't want the Android built-in SMS program to receive SMS, we can set the priority of our own receiver, and cut off the broadcast after we accept the broadcast to prevent it from being received by the Android built-in SMS program.

2、 Send SMS

1. Obtain the receiver's number and SMS content 2. Obtain the SMS sending management instance 3. Construct pendingintent to start the SMS sending status monitoring broadcast 4. Call the SMS sending function and send the SMS with parameters 5. Construct the internal class of the broadcast receiver to monitor whether the SMS is sent successfully 6. Obtain the broadcast receiver instance and intentfilter instance, and register the broadcast receiver 7 Cancel the registered broadcast receiver in ondestroy(). 8. Add the SMS sending permission in the androidmanifest.xml file

Next, put the specific layout file and code activity_ send_ Msg.xml file

Sendmsgactivity.java file

Add SMS sending permission in androidmanifest.xml file < uses permission Android: name = "Android. Permission. Send_sms" / >

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>