blackberry发送短信的应用可以调用
javax.wireless.messaging.MessageConnection;
来完成。
我首先尝试了用用数据报datagram发送2进制文件的形式,在我的黑莓8800上测试,遇乱码。
public SendSms() { try { _dc = (DatagramConnection)Connector.open(_openString); byte [] data = msg.getBytes(); Datagram d = _dc.newDatagram(_dc.getMaximumLength()); d.setAddress("//" + addr); _dc.send(d); } catch ( IOException e) { }S ystem.exit(0); }
然后改用javax.wireless.messaging.MessageConnection;的形式
成功发送任意字符(包括中文)
代码如下:
package com.kennan.contactList; import javax.wireless.messaging.MessageConnection; import javax.wireless.messaging.TextMessage; public final class Sender extends Thread { private MessageConnection _mc; private String _address; private String _message; public Sender(MessageConnection mc) { this._mc = mc; start(); } public synchronized void send(String addr, String msg) { _address = addr; _message = msg; notify(); } public synchronized void run() { while (true) { // If no client to deal, wait until one connects if (_message == null) { try { wait(); } catch (InterruptedException e) { } } try { TextMessage msg; // Are we a sender thread for the client ? If so then there's // no address parameter if (_address == null) { msg = (TextMessage) _mc .newMessage(MessageConnection.TEXT_MESSAGE); } else { msg = (TextMessage) _mc.newMessage( MessageConnection.TEXT_MESSAGE, _address); } msg.setPayloadText(_message); _mc.send(msg); } catch (Exception ioe) { ioe.printStackTrace(); } // Completed client handling, return handler to pool and // mark for wait _message = null; } } }
调用代码
package com.kennan.contactList; import java.io.IOException; import javax.microedition.io.Connector; import javax.wireless.messaging.MessageConnection; public class SendSMS { public static void sendSMS(String phoneNumber, String messageContext) { try { MessageConnection mc = (MessageConnection) Connector .open("sms://" + phoneNumber); Sender sender = new Sender(mc); sender.send(null, messageContext); } catch (IOException e) { e.printStackTrace(); } } }