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
Regular Contributor
musmankhan90
Posts: 92
Registered: ‎03-09-2013
My Device: bold 9800

SMS Receiving Application Sample Code

Good Day Everyone,

 

I am working on an application to send and receive sms's using the same application. I am able to send SMS and receive its delievery report using the same Sample Application Code available in Blackberry Sample SDK, but i am not able to receive incoming SMS's from any sender.. 

 

Please help any one to send me a link related to Incoming SMS receiving, i just want to show Dialog box or Textview when i received any incoming SMS.. Or please share sample code for Incoming SMS.

 

Thanks in advance..

 

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: SMS Receiving Application Sample Code

does this help?
http://supportforums.blackberry.com/t5/Java-Development/Different-ways-to-listen-for-SMS-messages/ta...
----------------------------------------------------------
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
Regular Contributor
musmankhan90
Posts: 92
Registered: ‎03-09-2013
My Device: bold 9800

Re: SMS Receiving Application Sample Code

Dear Simon,

 

I have already seen this article.. I just want to use this in within my sample "SMS Demo" application. In which if any one sends me an sms then it should display in front of the same application. I am not able to get the received sms. I am just getting Delievery report of my sending SMS.. Kindly help me out there, with any code snippet or sample code for reveiving SMS.

 

Thanks in advance

Regular Contributor
musmankhan90
Posts: 92
Registered: ‎03-09-2013
My Device: bold 9800

Re: SMS Receiving Application Sample Code

I am using this code snippet.. Within the same application i want to receive application whether it could be displayed on Textview or a Dialog Box.. Please kindly help me out here.. It will be very appreciated..

 

public class SMSDemo extends UiApplication
{
private static final int MAX_PHONE_NUMBER_LENGTH = 32;

private static final String NON_ZERO_PORT_NUMBER = "3590";

private EditField _sendText;
private EditField _address;
private EditField _status;
private ListeningThread _listener;
private SendThread _sender;
private Connection _conn;
private String _port = "0";


private StringBuffer _statusMsgs = new StringBuffer();



private static boolean isCDMA()
{
return (RadioInfo.getActiveWAFs() & RadioInfo.WAF_CDMA) == RadioInfo.WAF_CDMA;
}


public static void main(String[] args)
{

 

SMSDemo smsDemo = new SMSDemo();
smsDemo.enterEventDispatcher();
}


private class ListeningThread extends Thread
{
private boolean _stop;

private synchronized void stop()
{
_stop = true;

try
{
if( _conn != null )
{
_conn.close();
}
}
catch (IOException ioe)
{
}
}

 

 

public void run()
{
try
{
_conn = Connector.open("sms://:" + _port);
for(;:smileywink:
{
if ( _stop )
{
return;
}
MessageConnection msgConn = (MessageConnection)_conn;
Message m = msgConn.receive();

receivedSmsMessage(m);
}
}
catch (IOException ioe)
{
updateStatus(ioe.toString());
}
}
}

/**
* A simple abstraction of an SMS message, used by the SendThread class
*/
private static final class SmsMessage
{
private String _address;
private String _port;
private String _msg;


/**
* Creates a SMS message
* @param address The address of the recipient of the SMS message
* @param msg The message to send
*/
public SmsMessage(String address, String msg, String port)
{
_address = address;
_port = port;
_msg = msg;
}


/**
* Returns a Message object representing this SMS message
* @param mc The MessageConnection source with which to create the Message from
* @return The Message object representing the SMS message
*/
public Message toMessage(MessageConnection mc)
{
// If the user chose to have messages routed to the inbox (port = 0),
// we need to specify an address without a port number.
String addressString = "//" + _address + ( _port.equals(NON_ZERO_PORT_NUMBER) ? ":" + _port : "" );

TextMessage m = (TextMessage) mc.newMessage(MessageConnection.TEXT_MESSAGE , addressString);
m.setPayloadText(_msg);

return m;
}


public Datagram toDatagram(DatagramConnectionBase datagramConnectionBase) throws IOException
{
DatagramBase datagram = null;
byte[] data = _msg.getBytes("ISO-8859-1");
datagram = (DatagramBase) datagramConnectionBase.newDatagram();
SmsAddress smsAddress = new SmsAddress("//" + _address);
SMSPacketHeader smsPacketHeader = smsAddress.getHeader();
smsPacketHeader.setMessageCoding(SMSPacketHeader.MESSAGE_CODING_ISO8859_1);
datagram.setAddressBase(smsAddress);
datagram.write(data, 0, data.length);

return datagram;
}
}


/**
* A thread to manage outbound transactions
*/
private class SendThread extends Thread
{
private boolean _stopped = false;

// Create a vector of SmsMessage objects with an initial capacity of 5.
// For this implementation it is unlikely that more than 5 msgs will be
// queued at any one time.
private Vector _msgs = new Vector(5);


/**
* Queues message send requests to send later
* @param address The address to send the message to
* @param msg The message to send
*/
public void send(String address, String msg, String port)
{
SmsMessage message = new SmsMessage(address, msg, port);
synchronized (this._msgs)
{
if (! this._stopped)
{
this._msgs.addElement(message);
this._msgs.notifyAll();
}
}
}


/**
* Stops this thread from sending any more messages
*/
public void stop()
{
synchronized (this._msgs)
{
this._stopped = true;
this._msgs.notifyAll();
this._msgs.removeAllElements();

try
{
if ( _conn != null )
{
_conn.close();
}
}
catch (IOException ioe )
{
}
}
}

/**
* Sends any queued messages until stop() is called
* @see #stop()
* @see java.lang.Runnable#run()
*/
public void run()
{
while (true)
{
final SmsMessage smsMessage;
synchronized (this._msgs)
{
if (this._stopped)
{
return;
}
else if (this._msgs.isEmpty())
{
try
{
this._msgs.wait();
}
catch (InterruptedException ie)
{
return;
}
}

if (this._stopped)
{
return;
}
else
{
smsMessage = (SmsMessage) this._msgs.elementAt(0);
this._msgs.removeElementAt(0);
}
}
try
{
if(isCDMA())
{
DatagramConnectionBase dcb = (DatagramConnectionBase)_conn;
dcb.send(smsMessage.toDatagram(dcb));
}
else
{
MessageConnection mc = (MessageConnection)_conn;
mc.send(smsMessage.toMessage(mc));
}
}
catch (IOException ioe)
{
updateStatus(ioe.toString());
}
}
}
}


/**
* This screen acts as the main screen to allow the user to send and
* receive messages.
*/
private class SmsDemoScreen extends MainScreen
{
/**
* Default constructor
*/
private SmsDemoScreen()
{
setTitle("SMS Demo");

_address = new EditField("Destination:", "", MAX_PHONE_NUMBER_LENGTH, EditField.FILTER_PHONE);
add(_address);
_sendText = new EditField("Message:", "");
add(_sendText);
add(new SeparatorField());

_status = new EditField(Field.NON_FOCUSABLE);
add(_status);

// Sends an SMS message
MenuItem sendMenuItem = new MenuItem(new StringProvider("Send"), 0x230010, 0);
sendMenuItem.setCommand(new Command(new CommandHandler()
{
/**
* @see net.rim.device.api.command.CommandHandler#execute(ReadOnlyCommandMetadata, Object)
*/
public void execute(ReadOnlyCommandMetadata metadata, Object context)
{
String text = _sendText.getText();
String addr = _address.getText();

if (addr.length() == 0)
{
Dialog.alert("Destination field cannot be blank");
_address.setFocus();
}
else if(text.length() == 0)
{
Dialog.alert("Message field cannot be blank");
_sendText.setFocus();
}
else
{
_sender.send(addr, text, _port);
}
}
}));

addMenuItem(sendMenuItem);
}



public boolean onSavePrompt()
{
return true;
}



public void close()
{
_listener.stop();
_sender.stop();

super.close();
}
}

/**
* Creates a new SMSDemo object
*/
public SMSDemo()
{
invokeLater(new Runnable()
{

public void run()
{
if(!isCDMA())
{
int result = Dialog.ask(Dialog.D_YES_NO, "Send messages to inbox?", Dialog.YES);
if (!(result == Dialog.YES))
{
// If user chooses to not have message routed to inbox,
// we need to specify an arbitrary non-zero port number.
_port = NON_ZERO_PORT_NUMBER;
}
}

_listener = new ListeningThread();
_listener.start();

_sender = new SendThread();
_sender.start();
}
});

SmsDemoScreen screen = new SmsDemoScreen();
pushScreen(screen);
}

/**
* Update the GUI with the data just received
* @param msg The new status message to display on screen
*/
private void updateStatus(final String msg)
{
System.err.println(msg);

invokeLater(new Runnable()
{
/**
* Updates the GUI's status message
* @see java.lang.Runnable#run()
*/
public void run()
{

// Clear the string buffer
_statusMsgs.delete(0, _statusMsgs.length());

_statusMsgs.append(_status.getText());
_statusMsgs.append('\n');
_statusMsgs.append(msg);
_status.setText(_statusMsgs.toString());
}
});

}

/**
* Some simple formatting for a received SMS message
* @param m The message just received
*/
private void receivedSmsMessage(Message m)
{
String address = m.getAddress();
String msg = null;

if ( m instanceof TextMessage )
{
TextMessage tm = (TextMessage) m;
msg = tm.getPayloadText();
}

StringBuffer sb = new StringBuffer();
sb.append("Received:");
sb.append('\n');
sb.append("Destination:");
sb.append(address);
sb.append('\n');
sb.append("Data:");
sb.append(msg);
sb.append('\n');

updateStatus(sb.toString());
}

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: SMS Receiving Application Sample Code

I am not clear on what you are having problems with.

 

Looking at this code, the ListeningThread, receives the message, passes it to receivedSmsMessage() which extracts the required payload and then passed it on to updateStatus() which updates the Ui.  By updating updateStatus as you require, you can change where the text is displayed.  What problems are you having doing that?