使用j2me从黑莓中的后台线程发送短信

时间:2010-06-16 07:13:31

标签: blackberry java-me

嘿,我做了很多搜索,发现了一些类似的代码。 我试过gsm

方法1给出了IllegalArgumentException

try
{
MessageConnection _mc = (MessageConnection)Connector.open("sms://");
TextMessage tm = (TextMessage) _mc.newMessage(MessageConnection.TEXT_MESSAGE);
tm.setPayloadText(smsText);
tm.setAddress("965xxxxxxx");
_mc.send(tm);
_mc.close();
}catch(exception e){}

方法2:给出java.lang.error异常

  try
    {
    MessageConnection _mc = (MessageConnection)Connector.open("sms://");
    TextMessage tm = (TextMessage) _mc.newMessage(MessageConnection.TEXT_MESSAGE,
"//9790XXXXXX");
    tm.setPayloadText(text);
    _mc.send(tm);
    _mc.close();
        }catch(Exception e){}

我认为问题在于地址 我也尝试过:但没有成功 + 91965xxxxxxx, 0091965xxxxxxx, 0965xxxxxxx

我的申请如何运作----

我创建了2个应用程序 -

1) Application 1 is a background app that is a System module as well as 
    startup application.
2) Another is a uiapplication

后台应用程序在后台运行。如果有来电,则在持久对象中设置一个标志值,并在检查该值为真后,将短信发送给不通过其进行呼叫的短信。

1 个答案:

答案 0 :(得分:1)

好吧试试这个

import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.DatagramConnection;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

import net.rim.device.api.system.RadioInfo;

public class SendSMS extends Thread {

    private String to;
    private String msg;

    public SendSMS(String to, String msg) {
        this.to = to;
        this.msg = msg;

    }

    public void run() {
        if (RadioInfo.getNetworkType() == RadioInfo.NETWORK_CDMA) {
            DatagramConnection dc = null;
            try {
                dc = (DatagramConnection) Connector.open("sms://" + to);
                byte[] data = msg.getBytes();
                Datagram dg = dc.newDatagram(dc.getMaximumLength());
                dg.setData(data, 0, data.length);
                dc.send(dg);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    dc.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }

        } else {
            MessageConnection mc = null;
            try {
                mc = (MessageConnection) Connector
                        .open("sms://" + to);
                TextMessage m = (TextMessage) mc
                        .newMessage(MessageConnection.TEXT_MESSAGE);
                m.setPayloadText(msg);
                mc.send(m);
            } catch (IOException e1) {
                e1.printStackTrace();
            } finally {
                try {
                    mc.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());

                }
            }
        }
    }

}

并像这样打电话

public void callDisconnected(int callId) {
        final PhoneCall call = Phone.getCall(callId);
        final String number = call.getDisplayPhoneNumber();
        SendSMS sendSMS = new SendSMS(number, "message");
        sendSMS.start();
        super.callDisconnected(callId);
    }