Joke Collection Website - Public benefit messages - Development of GPRS short message for mobile phone

Development of GPRS short message for mobile phone

1. If you do GPRS SMS, you may only have socket. If it's just a general message, you can send it by SMS.

2. With regard to short messages, there are two situations, one is receiving and the other is sending. Let's take a look at sending first. The code is as follows:

public void sendImp() {

string addr = " SMS://"+phone . getstring()+":"+port . getstring();

Message connection connector;

string cont = content . getstring();

Try {

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

text message msg =(text message)conn . new message(message connection。 TEXT _ MESSAGE);

Msg.setPayloadText (continued);

conn . send(msg);

conn . close();

} catch (IOException e) {

// TODO automatically generated catch block

e . printstacktrace();

}

}

The key of the above code is to obtain the sending address, which consists of three parts: SMS://,phone number and local SMS port number. Usually this port number is obtained through getapp property ("SMS-port"). The rest is nothing special.

Look at the reception:

public void receiveImp() {

Message msg

String senderAddr

Try {

msg = conn . receive();

If (msg! = null) {

sender addr = msg . get address();

sender = senderAddr

If (message instance of text message) (

content = ((TextMessage)msg)。 getPayloadText();

}

Otherwise {

string buffer buf = new string buffer();

byte[] data = ((BinaryMessage)msg)。 getPayloadData();

for(int I = 0; I < data length; i++) {

int int data =(int)data[I]& amp; 0xFF

if(int data & lt; 0x 10) {

buf . append(" 0 ");

}

buf . append(integer . tohexstring(int data));

buf . append(“”);

}

content = buf . tostring();

System.out.println (content);

}

}

} catch(InterruptedIOException e){

// TODO automatically generated catch block

e . printstacktrace();

} catch (IOException e) {

// TODO automatically generated catch block

e . printstacktrace();

}

}

In j2me, there is an interface named MessageListener, which contains a method notification message (MessageConnection Conn). This method will be called and executed automatically once the message connection that registered it receives the message. Therefore, we should first create a new MessageConnection with the address of sms://port. This address is the same as sending a message, except the phone number is missing, and then use this connection to register the MessageListener interface. Then, write down the above receiving methods in notifyIncomingMessage, and you can receive the information normally.