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
GeekyDad
Posts: 56
Registered: ‎07-15-2008
My Device: Not Specified
Accepted Solution

Auto-Start and RuntimeStore usage for UIApplication... HELP!!

I have a UIApplication that runs in the background and uses an alternate entry point. The problem I am having is regarding device boot-up. The application will auto-start and be pushed into the RuntimeStore (it appears so anyway), and it will be running in the background once the device boots-up. But when I launch it the first time through the GUI, it tries to retrieve the existing instance from the RuntimeStore but comes back null. So, it instantiates the app again and places it in the RuntimeStore again, and then it will work as expected. Why would the RuntimeStore's put or get not work the first time when the app has auto-started? Any ideas?

 

Here's the code from my main UI app, which simply instantiaties my MainScreen class:

 

public class MyApp extends net.rim.device.api.ui.UiApplication { private static final long APP_ID = 0xf46f5a7867d69ff0L; public static void main(String[] args) { RuntimeStore runtimeStore = RuntimeStore.getRuntimeStore(); if (args != null && args.length > 0 && args[0].equals("gui")) { MyApp myApp = (MyApp)runtimeStore.get(APP_ID); if (myApp != null) { myApp.requestForeground(); } else { runtimeStore.put(APP_ID, new MyApp(true)); } } else { synchronized(runtimeStore) { runtimeStore.put(APP_ID, new MyApp(false)); } } } public MyApp(boolean isGui) { this.pushScreen(new MyAppScreen(isGui)); this.enterEventDispatcher(); } }

 

Please use plain text.
BlackBerry Development Advisor
MSohm
Posts: 14,649
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook

Re: Auto-Start and RuntimeStore usage for UIApplication... HELP!!

[ Edited ]
Try removing the synchronized(runtimeStore) block or instantiating the application outside of the synchronized block (the new MyApp(false) call).
Message Edited by MSohm on 07-17-2008 10:31 AM
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Developer
peter_strange
Posts: 19,607
Registered: ‎07-14-2008
My Device: Not Specified

Re: Auto-Start and RuntimeStore usage for UIApplication... HELP!!

I think your problem might be because

enterEventDispatcher();

never actually 'returns', so the RuntimeStore put does not complete.

 

Try the following code which I think does what you want.  I've put some System.out.println statements in so you can follows what is going on.  Put a breakpoint on the line after either of the enterEventDispatcher() to see if it actually gets there.

 

public class MyApp extends net.rim.device.api.ui.UiApplication { private static final long APP_ID = 0xf46f5a7867d69ff0L; public static void main(String[] args) { RuntimeStore runtimeStore = RuntimeStore.getRuntimeStore(); MyApp _myApp; if (args != null && args.length > 0 && args[0].equals("gui" ) ) { _myApp = (MyApp)runtimeStore.get(APP_ID); if (_myApp != null) { System.out.println("Forground" ); _myApp.requestForeground(); } else { System.out.println("Create" ); _myApp = new MyApp(true); runtimeStore.put(APP_ID, _myApp);

_myApp.enterEventDispatcher(); System.out.println("dispatched" ); } } else { System.out.println("Auto Start" ); _myApp = new MyApp(false); synchronized(runtimeStore) { runtimeStore.put(APP_ID, _myApp); } System.out.println("Added" ); _myApp.enterEventDispatcher(); System.out.println("dispatched" ); } } public MyApp(boolean isGui) { this.pushScreen(new MyAppScreen(isGui)); } }

 

Please use plain text.
Developer
GeekyDad
Posts: 56
Registered: ‎07-15-2008
My Device: Not Specified

Re: Auto-Start and RuntimeStore usage for UIApplication... HELP!!

Mark and Peter, thanks so much for your responses. I tried removing the "syncronized" block as Mark suggested, but that didn't help. I then tried Peter's suggestion of taking the enterEventDispatcher() call out of the constructor, and that did indeed fix it so it's now able to go into the RuntimeStore at auto-start. The problem now, however, is that when my main screen issues a super.close(), it doesn't shutdown the main app (MyApp) since the event dispatcher is now in the main method. At least, I'm assuming that is why. So, when the main screen has been closed and then the app is started again from the UI, it's finding an instance of MyApp still in the RuntimeStore, and therefore, tries to bring it to the foreground but can't since the screen no longer exists. Does that make sense?

 

Here's the code where the problem now lies:

 

_myApp = (MyApp)runtimeStore.get(APP_ID); if (_myApp != null) { System.out.println("Forground" ); _myApp.requestForeground();

 

 

Like I said, it's finding an instance of MyApp still in the RuntimeStore (when it should be null), and so it's trying to bring it to the foreground. What it should be doing is getting back a null from the RuntimeStore and then instantiating a new one. So, how can I shutdown the app when my MainScreen (MyAppScreen) issues super.close()? I've tried System.exit(0), but that didn't do it, either.

 

Any ideas?

Please use plain text.
Developer
peter_strange
Posts: 19,607
Registered: ‎07-14-2008
My Device: Not Specified

Re: Auto-Start and RuntimeStore usage for UIApplication... HELP!!

You have two choices.

 

1) You can leave the application in RuntimeStore and leave the Screen active, meaning it is there all the time.  To do this, the close becomes:

UiApplication.getUiApplication().requestBackground();

which pushes your screen to the background.

 

2) You can remove your RuntimeStore entry when your application finishes by changing your close as follows:

RuntimeStore runtimeStore = RuntimeStore.getRuntimeStore(); runtimeStore.remove(MyApp.APP_ID); System.exit(0);

Note that to use this I have removed the 'private' from the static APP_ID in MyApp.

 

In this sort of situation, I would consider doing both.  If the user 'escapes' or uses 'Close' then use Option (1), meaning they can quickly restart the app as they left it.  Also have an 'Exit' on the menu, that uses Option (2).

 

Just for fun, here is a sample myAppScreen, that does this.  Note the gui flag displayed too.....

 

public class MyAppScreen extends MainScreen { private MenuItem _closeMenu = new MenuItem("Close", 110, 10) { public void run() { close(); } }; private MenuItem _exitMenu = new MenuItem("Exit", 110, 10) { public void run() { exit(); } }; public MyAppScreen(boolean gui) { setTitle(new RichTextField("My App", Field.NON_FOCUSABLE)); add(new RichTextField("gui " + gui)); } public void close() { UiApplication.getUiApplication().requestBackground(); } void exit() { RuntimeStore runtimeStore = RuntimeStore.getRuntimeStore(); runtimeStore.remove(MyApp.APP_ID); System.exit(0); } protected void makeMenu( Menu menu, int instance) { menu.add(_closeMenu); menu.add(_exitMenu); } }

 

 

 

Please use plain text.
Developer
GeekyDad
Posts: 56
Registered: ‎07-15-2008
My Device: Not Specified

Re: Auto-Start and RuntimeStore usage for UIApplication... HELP!!

[ Edited ]

Thank you, Peter!!! I had already implemented #1 with sending my app to the background on close. The normal "Close" menu option or the escape button will send it to the background with the UI intact for quick return. I then have a "Shutdown" menu option which will actually exit the app. That's where I was calling the super.close() which was closing the main screen but not exiting the app completely. But your suggestion of using runtimeStore.remove() did the trick! I had actually thought about that as a possible solution, but got distracted trying other things. Thank you SO MUCH for all your help. I really do appreciate it.

 

My thanks to Mark, as well, for helping me work through this. After getting the enterEventDispatcher() properly placed, I was able to put the synchronized() block back in for the runtimeStore.get() call. It now works as it should.

 

Here's my working code to help conclude this thread:

public class MyApp extends net.rim.device.api.ui.UiApplication { public static final long APP_ID = 0xf46f5a7867d69ff0L; public static void main(String[] args) { MyApp myApp = null; RuntimeStore runtimeStore = RuntimeStore.getRuntimeStore(); boolean isGui = (args != null && args.length > 0 && args[0].equals("gui")); if (isGui) { synchronized (runtimeStore) { myApp = (MyApp)runtimeStore.get(APP_ID); } } if (myApp != null) { myApp.requestForeground(); } else { myApp = new MyApp(isGui); synchronized(runtimeStore) { runtimeStore.put(APP_ID, myApp); } myApp.enterEventDispatcher(); } } public MyApp(boolean isGui) { this.pushScreen(new MyAppScreen(isGui)); } }

 

And here are the close() and onClose() methods within MyAppScreen, which is my main screen:

public void close()
{
this.close(false);

public void close(boolean exitApplication) { if (exitApplication || this.onClose()) { RuntimeStore.getRuntimeStore().remove(MyApp.APP_ID); super.close(); } } public boolean onClose() { UiApplication.getUiApplication().requestBackground(); return false; }

 

Message Edited by GeekyDad on 07-17-2008 11:11 PM
Please use plain text.
New Developer
monternet
Posts: 58
Registered: ‎08-05-2009
My Device: Not Specified

Re: Auto-Start and RuntimeStore usage for UIApplication... HELP!!

Hi GeekyDad,

 

i will do the same thing what you have done: an application implements UiApplication, will be auto started after reboot, work as a background app, which listens the bluetooth serial port.

 

But after the installation BB went always dead. Can your give me any suggestion?

 

I'm not sure, what will happen after the installation of your app: a MyAppScreen will be created after device reboot but this MyAppScreen can't be seen until you klick the icon for your app on the desktop. Am I right?

 

Do you know how can i make it, if my background listening app will push a Screen to display the incoming data and this Screen should be automated displayed, even when BB is currently in "HomeScreen"?

 

 

Thank you in advanced!

 

regards!

 

monternet

Please use plain text.
Developer
peter_strange
Posts: 19,607
Registered: ‎07-14-2008
My Device: Not Specified

Re: Auto-Start and RuntimeStore usage for UIApplication... HELP!!

This Thread is solved, so people won't look at it.  I suggest you start a new Thread, and  link to this one if it has information that is relevant to you new Thread.

Please use plain text.