05-28-2012 05:36 AM
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.
Solved! Go to Solution.
05-29-2012 02:04 PM
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
05-30-2012 12:14 AM
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?
05-30-2012 12:32 AM
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
05-30-2012 12:59 AM
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.
05-30-2012 01:27 AM
thank you.Can you provide some code.I would rather prefer the OnClose method ovveride.What exactly i need to do inside that method
05-30-2012 01:36 AM
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.
05-30-2012 04:26 AM
Hi,
Here we go
public boolean onClose() {
UiApplication.getUiApplication().requestBackground
return true;
}
05-30-2012 05:52 AM
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.
05-30-2012 05:52 AM
Any idea