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

Web and WebWorks Development

Reply
New Contributor
Unsleddable
Posts: 9
Registered: ‎01-14-2012
My Device: Curve 9330

SMS Listener exception

We'll, I've tried just about everything and I can't figure this out. My application has a main page (index.html) and a background page (listener.html), as specified here:

 

<?xml version="1.0" encoding="UTF-8"?>
<widget xmlns="http://www.w3.org/ns/widgets" 
   xmlns:rim="http://www.blackberry.com/ns/widgets" 
   version="1.0.0.0" rim:header="SimpleSMS">
   <name>SimpleSMS</name>
   <description>Simple SMS</description>
   <content src="index.html">
       <rim:background src="listener.html" runOnStartup="true" />
   </content>
   <rim:navigation mode="focus" />
   <access subdomains="false" uri="http://jsconsole.com"/>
   <feature id="blackberry.message.sms" />
   <feature id="blackberry.app" />
   <feature id="blackberry.io.dir" />
   <feature id="blackberry.io.file" />
   <feature id="blackberry.utils" />
</widget>

 

 

The listener.html looks like this:

 

<!DOCTYPE html>
<html>
	<head>
	<script type="text/javascript" src="message_db2.js"></script>
	<script type="text/javascript">
	</script>
	</head>
	<body onload="initializeListener();">
	</body>
</html>

 

The initializeListener() function is as follows:

 

function initializeListener() {
	blackberry.message.sms.isListeningForMessage = true;
	blackberry.message.sms.addReceiveListener(messageListener);
}

 

The issue:

When I install my app, and the listener starts running, my app can receive text messages without problems. As soon as I open the main application, the problems begin. If I just minimize the app to the background using the back or end button, the next sms received causes an exception. If I close the app through the menu, there's no exception, but the listener ceases to function. I have commented all code in messageListener and index.html does absolutely nothing but display some html. There is almost no documentation regarding having a background page. Does anyone have any ideas? Thanks in advance.

Please use plain text.
New Contributor
Unsleddable
Posts: 9
Registered: ‎01-14-2012
My Device: Curve 9330

Re: SMS Listener exception

Forgot to add, Simulator is Curve 9330, OS 6.

Please use plain text.
Retired
jeffheifetz
Posts: 518
Registered: ‎07-18-2011
My Device: Bold

Re: SMS Listener exception

Hi Unsleddable,

 

Its important to note that the background page does not automatically get loaded when you exit the app (press the back key). It is just the page that is loaded when the app starts and does not foreground.

 

When you open the app it is supposed to now open the foreground page (ie index.html). If you do not have the listener code here the listener SHOULD stop working.

 

If you minimize the app nothing should change at all.

 

If you exit the app (press the back key) then the app is no longer running.

 

I hope this clarifies things.



1.Please resolve your thread by clicking the "Accept as Solution" button below the post which solved your problem!
2. If any post helps you please click the button below the post(s)
Please use plain text.
New Contributor
Unsleddable
Posts: 9
Registered: ‎01-14-2012
My Device: Curve 9330

Re: SMS Listener exception

[ Edited ]

Thanks, this explains almost everything. Would you mind elaborating on what exactly happens when the main app is launched? Is the background page killed, and then started again when the app is minimized? Or, is the background page paused/frozen, and then continues after the app is again minimized? Also, how can I know the background page is about to be closed, since leaving the listener in there seems to cause an exception, even when calling removeReceiveListener from the foreground page.

Please use plain text.
Contributor
coreb
Posts: 10
Registered: ‎02-24-2011
My Device: Bold 9700

Re: SMS Listener exception

I'd like an explanation of this as well. I attempted an answer when this was posted on StackOverflow. How off-base am I? Copy/paste of my answer below...

 

 

Could it be that webworks is going to these different pages and discarding everything that's in the previous page? (Like what happens to a form you fill out when you go to a new page in your browser)

Maybe you need to make use of Application Events to check when the app goes to the Foreground or Background; Maybe double check isListeningForMessage?

I'm thinking of something like

function onFG()
{
 
if(!blackberry.message.sms.isListeningForMessage)
 
{
    blackberry
.message.sms.isListeningForMessage =true;
    blackberry
.message.sms.addReceiveListener(messageListener);
 
}
}

blackberry
.app.event.onForeground(onFG);

You can also make use of isForeground boolean if you need to have your code distinguish where it is running.

Please use plain text.
Retired
jeffheifetz
Posts: 518
Registered: ‎07-18-2011
My Device: Bold

Re: SMS Listener exception

Hi Unsleddable and coreb,

 

I believe the workflow is something akin to the following. When the device loads, it will launch your app and point it to the background page. When you invoke the app from the ribbon, a new instance of the app will launch. This instance will tell the original instance to change to the foreground page, and then kill itself to make sure there is only ever one instance running.

 

As coreb points out, the exception you're seeing Unsleddable is likely caused by the fact that when you change pages you are in a brand new javascript context so none of the old javascript exists. Instead of de-registering the old listener on the foreground page, simply register a new one in its place.

 

As to an event, there is an onunload event to match the onload event, but I have no idea how reliable it is as the browser will likely force the change no matter what.

 

 



1.Please resolve your thread by clicking the "Accept as Solution" button below the post which solved your problem!
2. If any post helps you please click the button below the post(s)
Please use plain text.
Contributor
coreb
Posts: 10
Registered: ‎02-24-2011
My Device: Bold 9700

Re: SMS Listener exception

Thank you, that clarifies it greatly.
Please use plain text.
New Contributor
Unsleddable
Posts: 9
Registered: ‎01-14-2012
My Device: Curve 9330

Re: SMS Listener exception

Thanks for clarifying. I still get a Runtime exception as soon as I start the app and receive a new text. I tried removing the listener upon unload, yet I still get the same exception. When I look at the exception in the event log, it tells me that the exception took place somewhere in the SMS listener (seems like java code), and that the script has already been destroyed. Could java be trying to reference a de-allocated js method?

Please use plain text.