public void sendText( MessageConnection conn, String text )
throws IOException, InterruptedIOException {
TextMessage msg = conn.newMessage( conn.TEXT_MESSAGE );
msg.setPayloadText( text );
conn.send( msg );
}
Sending binary data is almost identical:
public void sendBinary( MessageConnection conn, byte[] data )
throws IOException, InterruptedIOException {
BinaryMessage msg =
conn.newMessage( conn.BINARY_MESSAGE );
msg.setPayloadData( data );
conn.send( msg );
}
//Receive.java
import java.io.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
MessageConnection conn = null;
String url = "sms://:5678"; // no phone number!
try {
conn = (MessageConnection) Connector.open( url );
while( true ){
Message msg = conn.receive(); // blocks
if( msg instanceof BinaryMessage ){
byte[] data =
((BinaryMessage) msg).getPayloadData();
// do something here
} else {
String text =
((TextMessage) msg).getPayloadText();
// do something here
}
}
}
catch( Exception e ){
// handle it
}
finally {
if( conn != null ){
try { conn.close(); } catch( Exception e ){}
}
}
j2me收发短信
最新推荐文章于 2019-02-25 18:23:11 发布