Joke Collection Website - Blessing messages - SMS sending is available in J2ME SMS, but SMS receiving is not available! That means you can't get the message! ! ! Master MM me QQ96222 187

SMS sending is available in J2ME SMS, but SMS receiving is not available! That means you can't get the message! ! ! Master MM me QQ96222 187

Wireless message API belongs to an optional package of J2ME system, usually abbreviated as WMA. It can be used to send and receive short messages or binary messages over wireless connections. WMA is based on the Universal Connection Framework (GCF), which is defined in the Connection Restricted Device Configuration (CLDC). GCF provides an abstract framework for performing input and output operations, and GCF is also a part of connected device configuration (CDC), so why use WMA on J2ME-enabled devices?

Using WMA means that you can use your mobile phone or similar device to send and receive short messages through various wireless networks, regardless of whether the wireless network is based on GSM or CDMA. It is worth mentioning that WMA also supports short message service (SMS) and cell broadcast short message service (CBS). Although WMA messages and datagrams are very similar, WMA does not use the datagraminterfaces defined in the GCF framework because the interfaces are designed for UDP connections. WMA defines a brand-new interface for sending and receiving short messages in javax.wireless.messaging package.

In WMA, in order to receive or send short messages, you must first obtain an instance of the MessageConnection interface. Call the open () method of javax.microedition.io.connector to return an instance of the MessageConnection interface, and the parameter is a word.

A string URL, which is very similar to the URL in the HTTP protocol. It must specify the protocol used (SMS or CBS), the number and port number of the target phone, etc. For example, the following URLs are all legal URLs:

1. SMS: //+4 1703496789 1

[]2. SMS://+417034967891:5678

3. SMS: //:5678

4. CBS: //:5678

[] In the URL string above, the first and second URL strings specify the number of the target device (mobile phone), and the second URL string also specifies the port number. If no port number is specified, WMA will use the default SMS port on the target device. The above two situations are mainly used to send short messages. The third URL string indicates that the port of this machine is specified, which is mainly used to receive short messages. The fourth URL string also specifies the port of this machine, which is mainly suitable for listening to broadcast information in the network. The main difference between the third URL string and the fourth URL string lies in the different protocols used.

[]

Let's look at a simple code to create an SMS client:

[] import javax.microedition.io. *;

Import javax.wireless.messaging. *;

.....

[]message connection conn = null;

string URL = " SMS://+4 1703496789 1 ";

attempt

{

conn =(message connection)connector . open(URL);

//Do something about the connection

}

Capture (exception e)

[]{

//Handling error

}

finally

{

If (conn! = empty)

{

}}

Now that the SMS client has been created, that is to say, the device in your hand has established a connection with the server, so how to send SMS? First, create an empty message using the newMessage () method of the MessageConnection interface, then set the PayloadText of the message (that is, the text or binary data to be sent), and finally call the send () method of the MessageConnection to send the short message to the target device. Please look at the following code:

Public void sendText (message connection connection, string text)

[]

Throw IOException, InterruptedIOException {

TEXT MESSAGE msg = conn . new MESSAGE(conn . TEXT _ MESSAGE);

Msg.setPayloadText (text);

conn . send(msg);

}

[] If you send data in binary format, the code will be slightly different:

Public void sendBinary (message connection connection, byte [] data)

Throw IOException, InterruptedIOException {

[]BINARY MESSAGE msg = conn . new MESSAGE(conn . BINARY _ MESSAGE);

Of course, the amount of data you can send is limited. Generally speaking, SMS text messages can contain 160 or 70 characters.

It depends on what character encoding you use. If it is binary data, the capacity is 140 bytes (note: WMA requires supporting message correlation, but this means that the limit is actually at least three times higher. ). You can also use numberO fSegments () of the MessageConnection interface.

Method determines whether a specific short message can be sent and how many message segments it needs to be divided into.

The above describes how to send short messages using WMA, and it is more convenient to receive short messages using WMA. When you open a server connection (you don't need to specify a phone number at this time, you only need to specify a protocol and a listening port number), you can directly call the receive () method of the MessageConnection interface, which returns the next short message received at the specified port of the current device. If no short message arrives, the method will block and wait for the next short message to arrive, otherwise another thread will close the connection. Please look at the following sample code:

Import java.io. *;

Import javax.microedition.io. *;

Import javax.wireless.messaging. *;

[]

MessageConnection conn = null

string URL = " SMS://:5678 "; //No phone number!

Try {

[]conn =(message connection)connector . open(URL);

while( true ){

message msg = conn . receive(); //block

If (message instance of binary message) (

Byte [] data =

((BinaryMessage) msg)。 getPayloadData();

//Do something here

} Otherwise {

[] String text =

((TextMessage) msg)。 getPayloadText();

[]

//Do something here

}}

}

Catch (exception e ){

//Deal with it

}

[]

Finally {if (conn! = null ){

Please try {conn.close (); } catch (exception e ){}

}

[]

}

WMA's task is only to receive and send short messages, not to interpret them. Generally speaking, applications send information of binary data types back and forth, such as data input stream, data output stream and byte array input stream of java.io package.

And ByteArrayOutputStream classes are very useful in decoding and encoding these binary data.