SingletonApp

Maybe below contain a lot of obvious questions... I went through a lot of reading but I could not yet answer.

I am trying to create an application that consists of a basic service and a user interface. The background service would be starts automatically when you start. So I used Singleton to develop the application.

Code is attached and it is very simple. There is no cleanup action in the example below.

The SingletonAppcontains a static variable that is the SingletonApp itself, and a method "createInstance".

When the entry point is called, it calls createInstance. If the _instance is null, create a new instance of SingletonApp, otherwise the existing one is reused.

Issues related to the:

1: whenever I click the icon, a new instance is always created and a new thread is started. Why the _instance is always null, even if it's a static variable?

2: I read some examples on the use of RuntimeStore, but it seems that my application does not need to store the application to the RuntimeStore. Below is written without using RuntimeStore intentionally. If I use RuntimeStore, my problem is simply not able to bring the screen interface by calling requestForeground(), I guess I missed something.

3: there are a few examples that check the args (autostartup, gui) to hand. Where are these passing arguments? Is the device / Simulator would be automatic passes these arguments according to different scenarios?

public class SingletonApp extends UiApplication {

    private static SingletonApp _instance = null;

    private SingletonApp() {
        pushScreen(new SingletonScreen());
    }

    public static synchronized SingletonApp createInstance() {

        if (_instance == null) {
            System.out.println("===== SingletonApp Creating Instance");
            _instance = new SingletonApp();
            _instance.enterEventDispatcher();
        } else {
            _instance.pushScreen(new SingletonScreen());
        }

        return _instance;

    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        SingletonApp app = createInstance();

    }

}
public class SingletonScreen extends MainScreen implements Runnable {

    public SingletonScreen() {
        setTitle("Hello World!");
        Thread t = new Thread(this);
        t.start();
    }

    public void run() {
        while (true) {
            int nCount = 0;
            try {
                System.out.println("===== Thread is running: " + ++nCount);
                Thread.sleep(5000);

            } catch (Exception e) {
                System.out.println("===== Exception@Run: " + e.getMessage());
            }
        }
    }
}

'For the approach, as we speak, so must override the close method of the screen so it sends the UiApplication back to bottom?'

Yes to the last screen displayed

"Then there's a running application that consists of a service"

There is not a separate background Application.  This is the same Application.  Foreground and background Applications can have listeners, Threads and so on.  There is no distinction between foreground and background UiApplications.  In fact, the only way you can test one

. isForeground().

"So, how know that starting the system or the application icon?

The application is never started from the icon.  The foreground only the application icon.  It's exactly the same way as if you were in your application, you spent to another task, then when in the home screen and clicked on the icon.

Tags: BlackBerry Developers

Similar Questions

Maybe you are looking for

  • Unable to diplay the image that is embedded in this html page

    <!DOCTYPE html> <html lang="en-US"> <head> <title style> Adding Hyperlink to Example3</title> <style> body {background-color:lightyellow} h1 {color:blue;} p.error { color: blue; } </style> </head> <body> <h1 > Example3</h1> <a href="http://www.google

  • My Thunderbird has stopped downloading messages - how can I allow him to run again. I restarted and downloaded a new version.

    I disconnected my antivirus (Norton) and tried again, without success. Thunderbird said I have 10 messages and try to download, but crashes and then upward and closes unexpectedly. It happened suddenly without apparent cause.

  • AT200 crashes playing music and the screen switch off

    Hello first of all, I want to apologize for my bad English.I ve got an AT-200 and I have this problem: when I listen to music and turn off the screen, the music begins to shake (like when you have a blue screen error in windows) and she don't brokes

  • Faces: Name einfügen EIB Fotos

    Hallo, ICH kann bei neuen Gesichtern nicht den Namen einfügen. Der auf das neue face works nicht DoubleClick. Das Fenster geht nicht auf equivalent. ICH habe schon El Capitan neu installed, ohne Erfolg. A staff yesterday please? Gruss Jörg

  • Measuring frequency

    Hi, first of all, happy new year! I have two problems and I hope that someone can help me. Well, I need to measure the frequency of ill-defined but periodic signals (filtered biopotentiels) and I have to do with an acquisition of data USB-6008. The m