简体   繁体   中英

How to quit / terminate / stop a j2me midlet?

Surprisingly terminating a midlet doesn't work in my application. Maybe it is because I'm using Threads, but destroyApp() and notifyDestroyed() are not sufficient.

Take for example the following code:

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    System.out.println("destroying");
    notifyDestroyed();
}
protected void startApp() throws MIDletStateChangeException {
   try {
        // init modules
        controller.initialize();
    }catch (Exception e) {
        viewer.showAlert("error in startApp() init controller");
        destroyApp(true);
    }

}

You are specifically calling notifyDestroyed() from inside startApp() .

My best guess is that the handset (or emulator) you are trying this on doesn't handle it too well.

Try this instead:

  • When controller.initialize() throws an exception, display a simple Form with a single "Exit" Command and a StringItem error message.

  • Call notifyDestroyed() from a CommandListener.commandAction() callback.

As far as threads are concerned, it is up to you to have them nicely terminate when the user wants to exit your application.

Most MIDP runtimes will be able to deal with some threads not terminating nicely but leaving system resources not properly cleaned may cause problems, especially on platforms that try to never terminate the Java Virtual Machine process itself.

您应该调用“ notifyDestroyed”方法退出应用程序,而不是“ destroyApp”方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM