The BlackBerry Developer

Friday, July 14, 2006

Launch an external application

I'll start off by saying this is not intuitive. It is especially difficult if the external application cannot be installed on the simulator (Thank you Yahoo Instant Messenger for Blackberry). I used the logic: 1.) show the app if it is already running, 2.) launch the app if it is not running, and 3.) show the download page if it isn't installed. You will need 4 things:

1. The app name.
The text it shows on the bottom of the ribbon when you select the icon.

2. The module name.
Go to Options -> Advanced -> Applications, select the app and select view properties. There may be more than one module in which case you'll have to figure out which one is the main app.

3. The application arguments.
I found this by debugging and examining the currently running Application Descriptors
4. The download URL.

Once you have these I used this code to check if the application is running and bring it to the front if it is:

ApplicationManager manager = ApplicationManager.getApplicationManager();

//Check to see if application is running.
ApplicationDescriptor descriptors[] = manager.getVisibleApplications();
//Retrieve the name of a running application.
for (int i = 0; i <>ApplicationDescriptor descriptor = descriptors[i];
if (descriptor.getName().equals( applicationName )) {
manager.requestForeground( manager.getProcessId( descriptor ) );

}
}

If it is not already running and you want to start it you need to get a handle on the module using this code:

int modHandle = CodeModuleManager.getModuleHandle( moduleName );

If that returns a number greater than 0 than you have a module handle id. That can be used to get an application descriptor which can be used to launch the application.
ApplicationDescriptor[] apDes = CodeModuleManager.getApplicationDescriptors( modHandle ); ApplicationDescriptor descriptor = apDes[0]; ApplicationManager.getApplicationManager().runApplication( descriptor );

If you want to add custom arguments, you will need to make your own descriptor. Since an ApplicationDescriptor object can only be created from an already existing descriptor, this code will do the trick:

ApplicationDescriptor newDescriptor = new ApplicationDescriptor(descriptor, descriptor.getName(), args);

If that doesn't work then you can launch the browser and direct them to the download page.

1 Comments:

Post a Comment

<< Home