简体   繁体   English

如何退出/终止/停止J2ME Midlet?

[英]How to quit / terminate / stop a j2me midlet?

Surprisingly terminating a midlet doesn't work in my application. 令人惊讶的是,终止Midlet在我的应用程序中不起作用。 Maybe it is because I'm using Threads, but destroyApp() and notifyDestroyed() are not sufficient. 可能是因为我使用的是Threads,但destroyApp()notifyDestroyed()不够。

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() . 您专门从startApp()内部调用notifyDestroyed() 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. controller.initialize()引发异常时,显示带有单个“退出” CommandStringItem错误消息的简单Form

  • Call notifyDestroyed() from a CommandListener.commandAction() callback. CommandListener.commandAction()回调调用notifyDestroyed()

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. 大多数MIDP运行时将能够处理某些线程,尽管它们不会很好地终止,但是未正确清理系统资源可能会导致问题,尤其是在试图永不终止Java虚拟机进程本身的平台上。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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