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
jobincantony
Posts: 26
Registered: ‎08-04-2008
My Device: Not Specified

Creating Auto-run applications on startup in blackberry with user control.( Hard and Soft reset)

[ Edited ]

Hi,

 

Blackberry JDE provides an option for making your application Auto-run on startup. But surprisingly these applications will auto-run only after hard reset (Alt + cap + Del or battery pull-up). But for a user perspective the application will restart on Soft reset too (turn off and turn on by pressing power button).

 

I have faced lot of issues for making a CLDC application auto-run in all the above cases and make  the auto-run option is user defined.

 

Now I have successfully implemented this and tested with OS versions 4.0.2 to 4.6.

 

I have made a monitor application with my Main application to do the Soft reset activity.

 

The monitor application is a CLDC application which extends the Application class and implements the SystemListener (for monitoring the powerUp() after reset) and GlobalEvent Listener (for  closing  the  monitor application).

 

Upon exiting from my main application I will check for Auto-run option enabled and if yes I will start the monitor application before exiting from mainApp. The monitor application will monitor the powerUp() status  after reset and will start the MainApplication. Main application will close the monitor application by posting a globalevent to monitor application.

 

The monitor application is a system module. So there is no ICON for it and we can add the .cod file of the application as a dependent module with our main application. The size of the monitor application is also very less (2k).

This  approach may help someone wants to make their CLDC application auto-run in all the scenarios.

 

Message Edited by jobincantony on 03-05-2009 09:52 AM
Please use plain text.
Developer
Posts: 432
Registered: ‎02-23-2009
My Device: Not Specified

Re: Creating Auto-run applications on startup in blackberry with user control.( Hard and Soft reset)

Auto run applications will always on device as we also check the system module option to make it autorun app. One morething you mention that you check the autorun option at runtime how you do that? i dont think so that you can do this. If you need implement autorun app then check autorun and system module options in project properties.

 

 


Press the kudos button to thank the user who helped you.

If your problem was get solved then please mark the thread as "Accepted solution".

 

 

Deepesh Gupta
Please use plain text.
Developer
jobincantony
Posts: 26
Registered: ‎08-04-2008
My Device: Not Specified

Re: Creating Auto-run applications on startup in blackberry with user control.( Hard and Soft reset)

Deepesh,

 

"Auto run applications will always on device as we also check the system module option to make it autorun app".

 

It is working with hard reset only. If you exit from your application and go for a soft reset, the application will not invoke automatically. From my experience, check autorun and system module options in project properties option will work with Hard reset only.

 

"One morething you mention that you check the autorun option at runtime how you do that?"

 

Sorry .. actually i have check the GUI option for user to enable/disable the auto-run functionality. I don't know if there is any other way to know whether it is auto-run or not.

 

I will post the steps i have followed. May be this will help someone :smileyhappy:

 

1.       Create your main UI CLDC application, say MySampleApp. Select  Properties -> Application then select the

Options Auto-run on startup (This will make your application auto-run on hard reset) and System module (No entry point from Icon).

 

2.       Add a GUI option for enable/ disable the autorun option in your main project( MySampleApp).

 

3.       Create a persistant store for storing the status of auto-run option.

 

4.       Create an alternate entry point for your application.( Create  a new project , say MySampleAppAlt . Go to Properties-> Application then select project type “Alternate CLDC application entry point” and Select “MySampleApp” as Alternate entry point for: . Add an argument say “fromGUIi”  to  arguments passed to option.)

 

5.       Add your application Icon file in MySampleAppAlt  project and set as application Icon.

 

 

6.       Create a dummy or monitor Say “SampleAppMonitor” CLDC application with SystemModule option selected ( This will remove the default Icon ) which extends Application and implements GlobalEvent Listener and SystemListener interfaces.

 

7.       Override the PowerUp() method of SystemListener with this code

           

int modHandle = CodeModuleManager.getModuleHandle("MySampleApp "); ApplicationDescriptor[] apDes = CodeModuleManager.getApplicationDescriptors(modHandle); ApplicationManager.getApplicationManager().runApplication(apDes[0]);

 

 

8.       Override the eventOccurred () method of SystemListener with this code

if (guid == 0x12345678) { System.exit(0); }

 

 

9.        Change your exitApplication() ( your exit point of  main application ie; MySampleApp) or onClose() to start    SampleAppMonitor upon exiting from your application.   

If(AutoRun) // store the status of user selection in AutoRun variable { int modHandle = CodeModuleManager.getModuleHandle("SampleAppMonitor "); ApplicationDescriptor[] apDes = CodeModuleManager.getApplicationDescriptors(modHandle); ApplicationManager.getApplicationManager().runApplication(apDes[0]); }

 

10.   In public static void main(String[] args) of MySampleApp, get the status of Auto-run from persistant store and  say it is boolean AutoRun. Add    

if (args != null && args.length > 0 && args[0].equals("FromGUI")) { dosStartUpProcess();// continue with your application. Entry from alternate entry point. } else if( false == AutoRun) // hard Reset and autorun option disabled by user. { System.exit(1); }

 

11.   Post a global event from your main application ( MySampleApp) to close the SampleAppMonitor. You can post this event from main just after enter your main application    

//for closing SampleAppMonitor application ApplicationManager appMgr = ApplicationManager.getApplicationManager(); int moduleHandle = CodeModuleManager.getModuleHandle("SampleAppMonitor "); ApplicationDescriptor[] appDes = CodeModuleManager.getApplicationDescriptors(moduleHandle); int processId = appMgr.getProcessId(appDes[0]); appMgr.postGlobalEvent(processId, 0x12345678, 0, 0,null,null);

 

 

 

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

Re: Creating Auto-run applications on startup in blackberry with user control.( Hard and Soft reset)

Excellent post jobincantony.

 

Just to confirm, auto-run is only used in hard reset.  System module is, as far as I am aware, not related to restart (soft or hard) at all.

 

We have applications that auto-run and cope with hard and sort resets, without the requirement for the separate monitor application and with fewer alternate entry points.  If I get the opportunity, I will try to summarize how these differ from what jobincantony suggests. 

 

However the functional requirements are the same.  While the solution presented in this Thread may have more 'bits' (classes and applications) than we use, that is only because each of the bits has been separated into different classes whereas what we do has them combined (and so is probably a little more complicated to understand).

Please use plain text.
Contributor
kellogs
Posts: 14
Registered: ‎06-19-2011
My Device: simulator Torch 9800
My Carrier: wired

Re: Creating Auto-run applications on startup in blackberry with user control.( Hard and Soft reset)

Hi,

 

is the code posted above still actual ? I am targeting OS 5.0+ and after reading about 5 threads on this topic and scouring through the KB I still do not know what the easiest way would be for achieving one single UiApplication app's autostart in both hard and soft reset scenarios. Its appearence under the task switcher is desired.

 

Peter, am I right to understand from your other posts that in my use case I do not need another Application derivedclass in my app ?

 

Sorry for the eventual redundance but I am unable to draw any conclusions at this time.

 

Thanks

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

Re: Creating Auto-run applications on startup in blackberry with user control.( Hard and Soft reset)

The specific requirement this post addresses is having an application that runs at start-up but can be shutdown by the user.  It will however be run at start-up again.  This is not a use case that I normally work to, in fact I can't think of an application that I have written that did this.  Typically applications will start at start-up and stay alive.  However the disadvantage of this approach is that a soft power down will kill things like network Threads, so if you do have an application that runs all the time, I think it is appropriate to shut it down when the user shuts off their device. 

 

To do this, what I would do is have the application register itself as a System Listener.  It uses the powerUp and powerOff to detect start-up and shutdown.  In these methods it should do what needs to be done, for example, stop network Threads, remove listeners, tidy up RuntimeStore and the like.  The trick with this is that the Application must NOT exit, i.e. must not call System.exit().  That will close the application and as a result, it will loose its SystemListener.

 

Hopefully this makes sense. 

Please use plain text.
Contributor
kellogs
Posts: 14
Registered: ‎06-19-2011
My Device: simulator Torch 9800
My Carrier: wired

Re: Creating Auto-run applications on startup in blackberry with user control.( Hard and Soft reset)

Again, what I am after is a GUI app (UiAplication) that would autostart / not autostart on boot depending on settings, can be closed with the "Close" menu item that gets added to its one and only screen or by any other means that fit into blackberry user experience , shows itself in taskbar and has a regular icon from where users can manually launch the application.

 

My understanding of what and how needs to be done:

 

1. Tick the 'Auto-run on startup' checkbox from BlackBerry_App_Descriptor.xml

2. type in an "Application argument" in BlackBerry_App_Descriptor.xml. This argument will come through the one and only main() static method when phone was hard-reset.

3. in the static main() method, register a listener for phone startup as described here - http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800901/How_to_-_Detec...

4. also in main(), the UiApplication, implmenting SystemListener adds itself as a listener.

5. Tidy up in PowerOff() as you have suggested

6. proceed as in step 3 on PowerUp()

?7? where do I need to make sure System.exit() does not get called ? As previously stated, users may quit this UiApplication

 

Thanks again!

Please use plain text.
Contributor
kellogs
Posts: 14
Registered: ‎06-19-2011
My Device: simulator Torch 9800
My Carrier: wired

Re: Creating Auto-run applications on startup in blackberry with user control.( Hard and Soft reset)

Hmm, are you basicaly telling me that once user has quit the UiApplication, there is no way for it to autostart on soft reset (but will autostart on hard reset nevertheless) ?
Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: Creating Auto-run applications on startup in blackberry with user control.( Hard and Soft reset)

Important points to note:

 

a) Apps can be set to autostart on hard reset of the device, but not on soft reset

b) Apps can 'stay alive' over a soft reset, but some processing can not (such as network I/O)

c) It is possible to have an Application that will listen for and react to soft power up and power down, but these apps must be registered

d) An app that closes itself completely, removes itself from the list of apps that are notified of soft powerUp or Down

e) It is possible to make an app look like it is closed by requesting it is put in the background.  This will remove it from the screen.  It will still be in the task switcher. 

f) Pressing the icon associated with an app will start the app, but if it is already started by in the background, it will be bought to the foreground.  The same thing can be achieved for an app in the Background by task switching to it. 

g) When starting after a hard reset, the recommended way to make sure that the device is ready for processing is to add a System Listener and leave the startup critical processing till the powerUp method is called.  You should leave things like network I/O or Ui processing. 

h) It is possible to remove the application from the task switcher, using the acceptsForeground method.  This will also stop the icon from bringing the app to the foreground. 

 

Now looking at your understanding

 

2. Is not needed - you only need to do this if you wish to distinguish between the autostart start-up and the icon start-up. 

3.  I prefer this link:

http://supportforums.blackberry.com/t5/Java-Development/Write-safe-initialization-code/ta-p/444795

6. Not sure what you mean here.

 

Regarding making sure that you don't do a System.exit, the OS may do one for you if you pop the last screen of the stack.  But if you just push the processing to the background rather than closing the screen, then you should be OK. 

 

Hope this helps.

Please use plain text.
Contributor
kellogs
Posts: 14
Registered: ‎06-19-2011
My Device: simulator Torch 9800
My Carrier: wired

Re: Creating Auto-run applications on startup in blackberry with user control.( Hard and Soft reset)

All right, thanks for your explanations. I guess there is no way for me of understanding a priori how to do this.. so I'll just wait for the signing keys and device owner's feedback on how it worked. Anyway, good points you have noted, thanks again!
Please use plain text.