Question

I have a background function listening for push messages. I need to handle though the push. I created the function to take any actions when the push arrives and it works pretty well. For example when a push arrives i increment a number etc etc.

However what would be the code to actually make the application start , when the user presses ok to the push?

I just need to make the application start normally , like the user just pressed on the icon of the app.

I am using OS < 7.X
Was it helpful?

Solution 2

try this - When you click the ok button use the following code to run your ui application.

public void dialogClosed(Dialog dialog, int choice) {
      switch (choice) {
          case Dialog.OK: 
              try {

                  ApplicationDescriptor[] appDescriptors =CodeModuleManager.getApplicationDescriptors(CodeModuleManager.getModuleHandle("BlackBerryCity"));    //here BlackBerryCity is the COD module Name
                  ApplicationDescriptor appDescriptor = new ApplicationDescriptor(appDescriptors[0], new String[] {"BlackBerryCity"});
                  ApplicationManager.getApplicationManager().runApplication(appDescriptor);

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

              break;
          case Dialog.CANCEL:

              break;
          default:
              break;
       }
   }

OTHER TIPS

One typical pattern is to build an application that has two entry points. That is, it can be started in two different ways. One way, would be the normal UiApplication. That's the standard BlackBerry app that can be started with a home screen icon press.

The other way would be to define a background service, that handles push notifications, and is started by the OS as soon as the device boots.

You'll define the background/push entry point by adding an Alternate Entry Point in your app's BlackBerry_App_Descriptor.xml file. Make sure to check Auto-run at Startup and Do not display the application icon .... Your app descriptor xml file should then contain something like this, in addition to the normal entry point for the UiApplication:

  <AlternateEntryPoints>
    <AlternateEntryPoint Title="PushService" MainMIDletName="" 
                         ArgumentsForMain="-push" HomeScreenPosition="0"
                         StartupTier="7" IsSystemModule="true" 
                         IsAutostartup="true" hasTitleResource="false" 
                         TitleResourceBundleKey="" TitleResourceBundleName="" 
                         TitleResourceBundleClassName="" TitleResourceBundleRelativePath="">
      <Icons/>
      <KeywordResources KeywordResourceBundleName="" KeywordResourceBundleRelativePath="" KeywordResourceBundleClassName="" KeywordResourceBundleKey=""/>
    </AlternateEntryPoint>
  </AlternateEntryPoints>

Then, you'll have a main program like this:

public class MyApp extends UiApplication

    public static void main(String[] args) {
       if (args.length > 0 && args[0].equals("-push")) {
          // this is the push service
          PushAgent pa = new PushAgent();
          pa.enterEventDispatcher();
       } else {
          // UiApplication
          MyApp app = new MyApp();
          app.enterEventDispatcher();
       }
    }
}

Where PushAgent is a class that extends Application, not UiApplication.

Then, when your push agent receives a notification and you decide you want to show the UI, use something like this:

ApplicationDescriptor ad = ApplicationDescriptor.currentApplicationDescriptor();
// String[] used for command line args, but we don't pass any to the UI app
ApplicationDescriptor ui = new ApplicationDescriptor(ad, new String[] { });
ApplicationManager.getApplicationManager().runApplication(ui);
class EntryPointForApplication extends UiApplication {

    private static EntryPointForApplication theApp;

    public EntryPointForApplication() { 
        GUIApplication scr = new GUIApplication(); 
        pushScreen(scr);         
    } 

}

Read this also How to setup alternate entry point in Blackberry application?

For sake of completeness here are all the options that you can use to launch an application:

I am assuming that you already have multiple entry points - one for the background listener and one for the UI Application. Also assuming that you are not passing any Application Arguments for the UI App. (See Nate's answer for full description of how to do this.)

  1. Using runApplication() method:

    ApplicationDescriptor ad = ApplicationDescriptor.currentApplicationDescriptor();
    // String[] used for command line args, but we don't pass any to the UI app
    ApplicationDescriptor ui = new ApplicationDescriptor(ad, new String[] { });
    //Launch the application and ask it to come in foreground
    ApplicationManager.getApplicationManager().runApplication(ui, true);
    
  2. Using launch() method:

    String modulename = "mymodule";
    ApplicationManager.launch(modulename);
    
  3. Using launchApplication() method:

    String modulename = "mymodule";
    ApplicationManager.launchApplication(modulename);
    

One thing to note is that if your UI app is already open, all these methods will simply bring it to foreground in whatever condition it it. If you require the click of button to open a new instance of your app, you will have to pass some random parameter as the application arguments and then ignore it in the main method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top