Unable to send SMS through custom SMS application.
Please Login to Remove!
Hi I am trying to developed a custom SMS application. I got some code snippet from other posts but I am still facing problem implementing the code. I tried two methods:
Method 1:
public class EnterNumber extends MainScreen {
LabelField titleEnterNumber = new LabelField("Enter Number",LabelField.ELLIPSIS);
HorizontalFieldManager hfm2 = new HorizontalFieldManager();
HorizontalFieldManager hfm3 = new HorizontalFieldManager();
BasicEditField msgno = new BasicEditField("Enter No:",null,10,TextField.PHONENUMBER);
public EnterNumber(){
setTitle(titleEnterNumber);
hfm2.add(msgno);
add(new SeparatorField());
add(hfm2);
}
protected void makeMenu(Menu menu, int instance){
menu.add(sendSms);
}
private MenuItem sendSms = new MenuItem("Send",110,10){
public void run(){
sendSMS();
}
};
public void sendSMS(){
Dialog.alert("inside sms");
String address = "sms://"+msgno.getText();
Dialog.alert(address);
System.out.println(address);
MessageConnection smsconn = null;
try {
smsconn = (MessageConnection) Connector.open(address);
TextMessage txtmessage = (TextMessage) smsconn.newMessage(MessageConnection.TEXT_MESSAGE) ;
txtmessage.setPayloadText("SMS sending suceessfull");
Dialog.alert("Messge is loaded");
System.out.println("Messge is loaded");
smsconn.send(txtmessage);
Dialog.alert("Message sent");
System.out.println("Message sent");
} catch (Exception t) {
System.out.println(t.getMessage());
Dialog.alert(t.getMessage());
System.out.println(t.getClass().getName());
}
if (smsconn != null) {
try {
smsconn.close();
} catch (IOException ioe) {
System.out.println(ioe.getMessage()); }
}
}
}
Using this I get an error alert after setPayloadText(): saying "blocking operation prohibited on event dispatcher thread".
So I used the Method 2 trying to run it in a thread:
public class EnterNumber extends MainScreen implements Runnable{
LabelField titleEnterNumber = new LabelField("Enter Number",LabelField.ELLIPSIS);
HorizontalFieldManager hfm2 = new HorizontalFieldManager();
HorizontalFieldManager hfm3 = new HorizontalFieldManager();
BasicEditField msgno = new BasicEditField("Enter No:",null,10,TextField.PHONENUMBER);
Thread t;
public EnterNumber(){
t = new Thread(this);
setTitle(titleEnterNumber);
hfm2.add(msgno);
add(new SeparatorField());
add(hfm2);
}
protected void makeMenu(Menu menu, int instance){
menu.add(sendSms);
}
private MenuItem sendSms = new MenuItem("Send",110,10){
public void run(){
Dialog.alert("inside run");
t.start();
}
};
public void run(){
Dialog.alert("inside sms");
String address = "sms://"+msgno.getText();
Dialog.alert(address);
System.out.println(address);
MessageConnection smsconn = null;
try {
smsconn = (MessageConnection) Connector.open(address);
TextMessage txtmessage = (TextMessage) smsconn.newMessage(MessageConnection.TEXT_MESSAGE) ;
txtmessage.setPayloadText("SMS sending suceessfull");
Dialog.alert("Messge is loaded");
System.out.println("Messge is loaded");
smsconn.send(txtmessage);
Dialog.alert("Message sent");
System.out.println("Message sent");
} catch (Exception t) {
System.out.println(t.getMessage());
Dialog.alert(t.getMessage());
System.out.println(t.getClass().getName());
}
if (smsconn != null) {
try {
smsconn.close();
} catch (IOException ioe) {
System.out.println(ioe.getMessage()); }
}
}
}
I only get one alert: "Inside run". After that no sms is sent, no error, no alert, no exception please help.
I am using following things for developing the application:
1. I am testing on BB 9000 handsets.
2. Using blackberry 4.6 plugin for Eclipse.
3. Error I am getting: Already mentioned with code.
4. Code: Already given both the implementation techniques.
METHOD 3:
I later tried a new approach trying to launch a new thread in constructor of a new class and tried to send SMS through that but the result was same as METHOD 2.
|