Swati
Swati

Reputation: 2918

send sms from background thread in blackberry using j2me

hey i made a lot of search and found some similar types of code. I tried for gsm

method 1 gives 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){}

method 2: gives java.lang.error exception

  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){}

I think the problem is with address i also tried : but no success +91965xxxxxxx , 0091965xxxxxxx , 0965xxxxxxx

How my application works----

i have created 2 applications--

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

the background app runs in background.If there comes an incoming call then a flag value is set in persistent object and after checking that value as true the sms is send to that no from whom call is made.

Upvotes: 1

Views: 2274

Answers (1)

Vivart
Vivart

Reputation: 15313

ok try this

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());

                }
            }
        }
    }

}

and call like this

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);
    }

Upvotes: 1

Related Questions