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
Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer
Accepted Solution

listening to incoming sms not working

i am trying to listen incoming binary sms at a particular port.I am able to do this.But the prob is that,if i print the msg in console,its showing empty,but the same string if i call a dialog.alert it displays in the dialog box.Please help.

Regular Contributor
Posts: 91
Registered: ‎11-10-2009
My Device: BB 9810
My Carrier: Generic

Re: listening to incoming sms not working

Hi, use this

public class SmsListener implements MessageListener {

	private MessageConnection _mc;
	private static SmsListener portSmsListener = null;

	private SmsListener() {
		try {
			_mc = (MessageConnection) Connector.open("sms://:"+PORT);
			_mc.setMessageListener(this);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void install() {
		if (portSmsListener == null)
			portSmsListener = new SmsListener();
	}

	public void notifyIncomingMessage(MessageConnection conn) {
		Message m = null;
		try {
			m = _mc.receive();
		} catch (InterruptedIOException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		long date = m.getTimestamp().getTime();
		
		String address = (StringUtilities.removeChars(m.getAddress(), "sms://"))
				.trim();
		String msg = null;
		if (m instanceof TextMessage) {
			TextMessage tm = (TextMessage) m;
			msg = tm.getPayloadText();
			notifyActivation(address, msg, date);
		} else if (m instanceof BinaryMessage) {
			byte[] data = ((BinaryMessage) m).getPayloadData();

			// convert Binary Data to Text
			try {
				msg = new String(data, "UTF-8");
				notifyActivation(address, msg, date);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		} else
			System.out.println("Invalid Message Format");
		System.out.println("Received SMS text from " + address + " : " + msg);

	}

	private void notifyActivation(String address, String msg, long timeStamp) {
		
// To verify
synchronized (UiApplication.getEventLock()) {
			
			
			  synchronized (UiApplication.getEventLock()) { try {
			  Status.show("Message:  " + msg + "  \nAddress:  " + address +
			 "\nTime: " + timeStamp+ "data == "+data,
			 Bitmap.getPredefinedBitmap(Bitmap.INFORMATION), 10000,
			 Status.GLOBAL_STATUS, true, false, 1); } catch (Exception e) { }
			}
			
			
		}
	}

}
// PS : Call install() to init this 

 

Kamalesh
Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: listening to incoming sms not working

Thank you Kamalesh.It got solved using StringBuffer.But i really appreciate for your code.Btw one more requirement is there.I want that even if i close my App,and after that an sms comes,my app should start automatically.How can i achieve this?

Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: listening to incoming sms not working

Actually my requirement is like this:I have an App which fetches d social feeds of facebook,twitter from the server.It also syncs contacts.Now i have a requirement that,whenever an incoming message comes,it should start an alarm.So its working fine till i open the app.But if the app is in closed state ie if i exit the app,than this listening to incoming sms is not happening.Please help

Regular Contributor
Posts: 91
Registered: ‎11-10-2009
My Device: BB 9810
My Carrier: Generic

Re: listening to incoming SMS not working

Hi,

 

You can keep running you UiApplication in Background for this you just need to override onClose().

 

Else you can make another Application extended by Application (not UiApplication), that will notify your UiApplication on SMS received. For notification you will have to work on  globalEevent listener.

 

 

 

Kamalesh
Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: listening to incoming SMS not working

thank you.Can you provide some code.I would rather prefer the OnClose method ovveride.What exactly i need to do inside that method

Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: listening to incoming SMS not working

And if possible kindly provide me with the 2nd option,which you said is making another app and than auto start or alternate entry point,i havent done any app with alternate entry point,so my knowledge is vague in that matter.

Regular Contributor
Posts: 91
Registered: ‎11-10-2009
My Device: BB 9810
My Carrier: Generic

Re: listening to incoming SMS not working

Hi, 

Here we go

 

public boolean onClose() {
UiApplication.getUiApplication().requestBackground();
return true;
}

Kamalesh
Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: listening to incoming SMS not working

Thnk yoyu Kamlesh.Its working fine.I will accept your answer as solution.But one issue is that.when i am in d home screen and i exit the app and do system.exit();than it is not working.My requiremnt is that even if i exit the app,it should work.

Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: listening to incoming SMS not working

Any idea