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: 79
Registered: ‎09-15-2008
My Device: Not Specified
Accepted Solution

Issue with Message Listener In Backgroundlisterner Thread for UI Application

Hi All,

 

I have a UI Appliation with Background Listener with interface of MessageListener and I am regestering it to receive Notification of incoming SMS, on a particular port,

But the  notifyIncomingMessage() is never called and sms always comes in inbox.

 

if am using port 8855, and

_mc = (MessageConnection)Connector.open("sms://:8855");
            _mc.setMessageListener(this);

 

 

Detail of My code.

I have a UI application with alternate Entry point, so in the Background i register mylistener, so that it notifys me of incoming message and then i can launch my UI application.

 

The Code :

My UI Application.

 

 

public static void main(String[] args) { if(args != null && args.length > 0 && args[0].endsWith("gui")) { MyUiApplication mb = new MyUiApplication(); mb.enterEventDispatcher(); } else { //Entered through the alternate application entry point BackgroundListener.waitForSingleton(); } }

 

 And My Background Lisnter looks like..

 

public class BackgroundListener implements PIMListListener, javax.wireless.messaging.MessageListener public BackgroundListener() { try { System.out.println("Adding the Message Listner"); //Implementing the Message Listner: _mc = (MessageConnection)Connector.open("sms://:8855"); _mc.setMessageListener(this); } catch(Exception e) { } } public void notifyIncomingMessage(MessageConnection conn) { System.out.println("BBBBBBB Received the SMS,Read Message Thread Start BBBBBBBBBB"); Logger.log(DEBUG, "BBBBBBB Received the SMS,Read Message Thread Start BBBBBBBBBB"); //startMessageReadThread(); new Thread() { MessageConnection connection; Thread set (MessageConnection con) { this.connection = con; return (this); } public void run() { try { Message m = connection.receive(); String msg = null; if (m instanceof TextMessage) { TextMessage tm = (TextMessage)m ; msg =tm.getPayloadText(); } if (msg.equals(CBConstants.SMS_CMD_SYNC)) { } //if () }catch(Exception error) { System.out.println(error.getMessage()); } } }.set(conn).start(); } public static BackgroundListener waitForSingleton() { //Ensure this is a singleton instance. //Open RuntimeStore and obtain the reference of BackgroundListener Logger.log(DEBUG, "In the Listener class"); RuntimeStore store = RuntimeStore.getRuntimeStore(); Object obj = store.get(RTSID_LISTENER_ID); //If obj is null, there is no current reference to BackgroundListener //Start a new instance of BackgroundLIstener if one is not running if(obj == null) { Logger.log(DEBUG, "In the Listener class obj is null so newBackgroundlistener"); store.put(RTSID_LISTENER_ID, new BackgroundListener()); return (BackgroundListener)store.get(RTSID_LISTENER_ID); } else { Logger.log(DEBUG, "In the Listener class, already a instance so will return the obj"); return(BackgroundListener)obj; } }

 Can any one please suggest me where i am doing wrong.

 

I also saw all the methods for sms listening, with datagram and messagelistener.

As this will contineously run in Background i thought of MessageListener.

Any help appreciated.

 

Thanks

 

Alkesh

 

 

Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: Issue with Message Listener In Backgroundlisterner Thread for UI Application

Is the Message you sending is having the port information which you listeneing.

Use Search. "Accept Solution" If the problem is resolved.
Developer
Posts: 79
Registered: ‎09-15-2008
My Device: Not Specified

Re: Issue with Message Listener In Backgroundlisterner Thread for UI Application

Yes, I am sending a Port Directed SMS on port "8855".

Even i tried a default port "0". And send a normal sms the message comes in the inbox but notifymessage is never called.

 

Thanks

 

Alkesh

Developer
Posts: 342
Registered: ‎02-04-2009
My Device: BB 8320

Re: Issue with Message Listener In Backgroundlisterner Thread for UI Application

Developer
Posts: 79
Registered: ‎09-15-2008
My Device: Not Specified

Re: Issue with Message Listener In Backgroundlisterner Thread for UI Application

Baskaraninfo,

 

The first link yor are refering is specific to Folder Listener for, email and Inbox stuff, and not the sms as per my understanding.

 

While the second on, i had gone through that a number of time now and i am using the same approach for it,

 

I want a listener to run contineous in background, so datagram will not be a good option.

As i am using this sms to triger my Ui application to launch.

 

Any other suggestion is a appreciated.

 

 

Highlighted
Developer
Posts: 30
Registered: ‎05-07-2009
My Device: Not Specified

Re: Issue with Message Listener In Backgroundlisterner Thread for UI Application

Can you test out receiving an SMS message without the fancy background stuff going on?  Namely, try to send the SMS to your app while it's just running in the foreground waiting to receive SMS messages.  That way you can isolate the problem to be SMS related as opposed to problems with the background app logic (which is somewhat confusing).

 

Try out this simple app.  It sits there waiting to receive SMS messages and adds a field to the screen when a message is received.  What happens in this app when you send the SMS to the BlackBerry device?

 

 

import java.io.IOException; import javax.microedition.io.Connection; import javax.microedition.io.Connector; import javax.wireless.messaging.Message; import javax.wireless.messaging.MessageConnection; import net.rim.device.api.ui.UiApplication; import net.rim.device.api.ui.component.RichTextField; import net.rim.device.api.ui.container.MainScreen; public class SMSReceiveTest extends UiApplication implements Runnable { private static final int SMS_PORT = 8855; private final MainScreen _screen; private Connection _connection; public SMSReceiveTest() { this._screen = new MyMainScreen(); this.pushScreen(this._screen); } private void log(String message) { final RichTextField field = new RichTextField(message); this.invokeAndWait(new Runnable() { public void run() { SMSReceiveTest.this._screen.add(field); } }); } public void run() { while (!this.hasEventThread()) { Thread.yield(); } this.log("Opening SMS connection on port " + SMS_PORT); final Connection con; try { con = Connector.open("sms://:" + SMS_PORT); } catch (final Exception e) { this.log("ERROR: Connector.open() threw " + e); return; } try { this.log("SMS connection opened successfully"); this._connection = con; final MessageConnection mc = (MessageConnection) con; while (true) { this.log("Waiting to receive SMS message"); final Message message; try { message = mc.receive(); } catch (final Exception e) { this.log("ERROR: receive() threw " + e); continue; } this.log("Received SMS from " + message.getAddress()); } } finally { try { con.close(); } catch (final IOException e) { // oh well } } } public static void main(String[] args) { final SMSReceiveTest app = new SMSReceiveTest(); new Thread(app).start(); app.enterEventDispatcher(); } private class MyMainScreen extends MainScreen { public MyMainScreen() { this.setTitle("SMS Receive Test"); } public void close() { if (SMSReceiveTest.this._connection != null) { try { SMSReceiveTest.this._connection.close(); } catch (final Exception e) { // oh well } } super.close(); } } }

 

 

 

Developer
Posts: 79
Registered: ‎09-15-2008
My Device: Not Specified

Re: Issue with Message Listener In Backgroundlisterner Thread for UI Application

Hi Cream8tor,

 

Thanks for the respose , it will give a try to this.

 

Thanks

 

Alkesh

Developer
Posts: 79
Registered: ‎09-15-2008
My Device: Not Specified

Re: Issue with Message Listener In Backgroundlisterner Thread for UI Application

I got the Application working,

 

I changed the port to "0", Though it comes in the inbox. But currently its acceptable for me, because the device doesnt receive it on port 8855.

 

Thanks

 

Alkesh

Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: Issue with Message Listener In Backgroundlisterner Thread for UI Application

Is it a problem with all port other than "0"?

Use Search. "Accept Solution" If the problem is resolved.
Developer
Posts: 79
Registered: ‎09-15-2008
My Device: Not Specified

Re: Issue with Message Listener In Backgroundlisterner Thread for UI Application

For me it was giving problem, when i was using port  "8855".

The notifyincomingmessage was never called.

 

I had a sample application sending the message on port 8855, and also a server implementation fo sending the same, so sending was not a issue.

 

Only notifyincomingmessage never got called.

 

Thanks

 

Alkesh