Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
vignaraj
Posts: 9
Registered: ‎05-18-2013
My Device: 9800
My Carrier: Airtel

How to Send the Text Message(SMS) to Multiple Numbers or Contacts?

 

package mypackage;

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;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class MyScreen extends MainScreen
{
/**
* Creates a new MyScreen object
*/
String[] numbers={"number1","number2","number3","number4"};
public MyScreen()
{ 
// Set the displayed title of the screen 
setTitle("Sending SMS");
LabelField ph_no_lbl= new LabelField("Enter the Phone Number");
LabelField msg_lbl= new LabelField("Enter the Message");
final EditField phone_number_edit= new EditField(Field.EDITABLE|FOCUSABLE);
final EditField message_edit= new EditField(Field.EDITABLE|FOCUSABLE);
ButtonField send = new ButtonField(Field.FOCUSABLE);
send.setLabel("Click Here to Send");
StringBuffer result = new StringBuffer();
for (int i = 0; i < numbers.length; i++) {
result.append( numbers[i]+"\n" );
//result.append( optional separator );
}
String mynewstring = result.toString();;
phone_number_edit.setText(mynewstring);
add(ph_no_lbl);
add(phone_number_edit);
add(msg_lbl);
add(message_edit);
add(send);
send.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
// final String getNumber= phone_number_edit.getText().toString();
final String getMessage= message_edit.getText().toString();
for(int i=0;i<numbers.length;i++)
{
if(numbers[i].length()==0 || numbers[i].length()<=9 || getMessage.length()==0)
{
Dialog.alert("Enter the Fields CareFully");
}
else
{
try
{
sendSMS(numbers[i], getMessage);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
});
}
public static void sendSMS(final String no, final String msg) {
// try {
new Thread() {
public void run() {
boolean smsSuccess = false;
if (RadioInfo.getNetworkType() == RadioInfo.NETWORK_CDMA) {

DatagramConnection dc = null;
try {
dc = (DatagramConnection) Connector.open("sms://" + no);
byte[] data = msg.getBytes();
Datagram dg = dc.newDatagram(dc.getMaximumLength());
dg.setData(data, 0, data.length);
dc.send(dg);
// / send successfully
smsSuccess = true;
} catch (Exception e) {
System.out.println("Exception 1 : " + e.toString());
e.printStackTrace();
smsSuccess = false;
} finally {
try {
dc.close();
dc = null;
} catch (IOException e) {
System.out.println("Exception 2 : " + e.toString());
e.printStackTrace();
}
}
} else {
MessageConnection conn = null;
try {
conn = (MessageConnection) Connector
.open("sms://" + no);
TextMessage tmsg = (TextMessage) conn
.newMessage(MessageConnection.TEXT_MESSAGE);
tmsg.setAddress("sms://" + no);
tmsg.setPayloadText(msg);
conn.send(tmsg);
smsSuccess = true;
} catch (Exception e) {
smsSuccess = false;
System.out.println("Exception 3 : " + e.toString());
e.printStackTrace();
} finally {
try {
conn.close();
conn = null;
} catch (IOException e) {
System.out.println("Exception 4 : " + e.toString());
e.printStackTrace();
}
}
}
if(smsSuccess)
{
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
// TODO Auto-generated method stub
Dialog.alert("Message Sending Succcessfully :-)");
}
}); 
}else
{
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
// TODO Auto-generated method stub
Dialog.alert("Message Sending Failure :-(");
}
});
}
}
}.start();
} 
}

 

 

This is my code i'm trying to send the SMS to multiple numbers from String array. I'm sending sms, but its send only first string of string array. Remaining String address will not be send. I dont know what's the problem?? Please help me to find out solution.

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to Send the Text Message(SMS) to Multiple Numbers or Contacts?

And what happens to your second (or later) requests?

 

Have you stepped through this code to make sure it is processing the way you want?

Please use plain text.
Developer
simon_hain
Posts: 16,089
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to Send the Text Message(SMS) to Multiple Numbers or Contacts?

as far as i know you have to send a single sms to each number, like a batch.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
New Contributor
vignaraj
Posts: 9
Registered: ‎05-18-2013
My Device: 9800
My Carrier: Airtel

Re: How to Send the Text Message(SMS) to Multiple Numbers or Contacts?

sendSMS(numbers[i], getMessage);

It's the process to send the SMS to multiple numbers, But it could send only First number of array.. Other numbers will not be send..
Please use plain text.
New Contributor
vignaraj
Posts: 9
Registered: ‎05-18-2013
My Device: 9800
My Carrier: Airtel

Re: How to Send the Text Message(SMS) to Multiple Numbers or Contacts?

No, Mr.simon. If i click send button, SMS will send the numbers in the array.. One by One..
Please use plain text.
Developer
simon_hain
Posts: 16,089
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to Send the Text Message(SMS) to Multiple Numbers or Contacts?

maybe add some wait time between sending them
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
New Contributor
vignaraj
Posts: 9
Registered: ‎05-18-2013
My Device: 9800
My Carrier: Airtel

Re: How to Send the Text Message(SMS) to Multiple Numbers or Contacts?

ya, but sending to every numbers in the array.

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to Send the Text Message(SMS) to Multiple Numbers or Contacts?

Can you answer the questions I asked previously?

 

"And what happens to your second (or later) requests?

 

Have you stepped through this code to make sure it is processing the way you want?"

Please use plain text.