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
Contributor
mail2bhramar
Posts: 14
Registered: ‎04-27-2011
My Device: 8900
My Carrier: Idea

Not able to read Incoming sms programmatically.

[ Edited ]

Hi Friendz.. as per my requirement i want to read incoing sms and start my application based upon that sms.my problem is i am not able to read incoming sms by these code.

public class MyApp extends Application {
	private ListeningThread _listener;

	public static void main(String[] args) {
		new MyApp().enterEventDispatcher();
	}

	MyApp() {
		_listener = new ListeningThread();
		_listener.start();
	}

	private static class ListeningThread extends Thread {
		private boolean _stop = false;
		private DatagramConnection _dc;

		public synchronized void stop() {
			_stop = true;
			try {
				_dc.close(); // Close the connection so the thread returns.
			} catch (IOException e) {
				System.err.println("stop().IOException: " + e.toString());
			}
		}

		public void run() {
			try {
				_dc = (DatagramConnection) Connector.open("sms://");
				for (;;) {
					if (_stop) {
						return;
					}
					Datagram d = _dc.newDatagram(_dc.getMaximumLength());
					_dc.receive(d);

					String address = new String(d.getAddress());
					String msg = new String(d.getData());
					System.out.println("Message received: " + msg);
					if(msg!=null){
					Dialog.alert("Hi" + msg);
					}
					System.out.println("From: " + address);
					System.exit(0);
				}
			} catch (IOException e) {
				System.err.println("run().IOException: " + e.toString());
			}
		}
	}
}

 As i do not have that much of experience in BB. i need your help to do the same .please give me some code  to do the same.

 

One more Thing is there any way to receive incoming sms from Emulator like in android emulator

 

Thanks In advance.

 

Thesimple
Please use plain text.
Contributor
magnetix
Posts: 41
Registered: ‎11-11-2010
My Device: Not Specified

Re: Not able to read Incoming sms programmatically.

Hi - have you tried this MessageListener example?

 

It seems to do what you are asking for.

Please use plain text.
Regular Contributor
kamaleshpandey
Posts: 91
Registered: ‎11-10-2009
My Device: BB 9810
My Carrier: Generic

Re: Not able to read Incoming sms programmatically.

Try 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
Please use plain text.
Contributor
mail2bhramar
Posts: 14
Registered: ‎04-27-2011
My Device: 8900
My Carrier: Idea

Re: Not able to read Incoming sms programmatically.

Thank you for your replay....i want to start showing the body of the sms in Dialog. may i know how to do this.

Thesimple
Please use plain text.
Developer
simon_hain
Posts: 15,947
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Not able to read Incoming sms programmatically.

to interact with the UI from a separate thread you need to use invokelater, see http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865
----------------------------------------------------------
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.