Small program example of sending SMS in Android Development

The above figure is the code structure diagram.

Now let's look at the specific code.

Send.java

main.xml

AndroidManifest.xml

The final effect drawing is:

Like the calling applet, you need to turn on two avds to test the function.

Broken thoughts:

The main class of SMS application is smsmanager. Before Android 2.0, you should use android.telephone.gsm.smsmanager

After that, you should use Android. Telephone. Smsmanager;

It means to get the default information manager of the system

smsManager.sendTextMessage(destinationAddress,scAddress,text,sentIntent,deliveryIntent)

--Destinationaddress: target telephone number -- schaddress: the SMS center number of the service provider (for example, the SMS center number of China Mobile). It can be left blank in the test-- Text: SMS content -- sendintent: send -- > China Mobile -- > China Mobile send failure -- > return send success or failure signal -- > subsequent processing, that is, this intention wraps the information of SMS sending status

--Deliveryintent: send -- > China Mobile -- > China Mobile successfully sent -- > return whether the other party has received this message -- > subsequent processing, that is, this intention packages the status information of whether the SMS has been received by the other party (the supplier has successfully sent it, but the other party has not received it).

Public static pendingintent getbroadcast (context, int requestcode, intent intent, int flags) returns a pendingintent for broadcasting, which is similar to calling the context. Sendbroadcast() function, requestcode is not used temporarily, and intent is an intent for broadcasting. Flags include: Flag_ ONE_ SHOT,FLAG_ NO_ CREATE,FLAG_ CANCEL_ CURRENT,FLAG_ UPDATE_ Current is used to set properties such as using the new pendingintent once, not creating if none, canceling the current, updating the current, etc.

In addition, we also declare the SMS sending permission in androidmanifest.xml.

<uses-permission android:name="android.permission.SEND_SMS"/>

Sometimes, when we simulate texting with two avds, we will find that sometimes the program can not be used normally. The system will prompt us no DNS servers found and the DNS service cannot be found. This situation is generally caused by the fact that your computer is not connected to the network.

Send SMS:

Display SMS writing interface:

Send email:

Upgraded version: this code adds the listening of broadcast receiver to it. The detailed code is as follows

Main.xml is the same as androidmanifest.xml and the previous code.

Registerreceiver() is used to register broadcast recipients. This method is defined in content.

public abstract Intent registerReceiver(BroadcastReceiver receiver,IntentFilter filter); If the system finds a broadcast that meets the filter, it will teach the receiver to handle it. It is generally handled in its onReceive () method.

If you do not actively register through registerreceiver () in the code, you need to configure it from androidmanifest.xml. The code is as follows

Note that in the configuration file, the activity tag and the receiver tag are at the same level.

When sending Chinese in the simulator, the receiver will have the problem of garbled code, but in the real machine, there will be no garbled code. Therefore, developers only need to develop SMS function normally without code conversion.

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
分享
二维码
< <上一篇
下一篇>>