question for terminate app in j2me but it doesnt m

2019-04-13 04:40发布

i have case in j2me .. i want terminate when app in process execute code. this is my simple code.

else if (c == cmdStop) {

                //command berhenti
                browser.stop();

            }

 public void stop(){
             // No errors
                  int errorCode = 0;

                 // An error occurred
                    errorCode = -1;

                  // Terminate
                       System.exit(errorCode);

            }

the problem is when i try to terminate app, the app still worked or continue execute and ignore function system.exit.

still excecute this code

private void paintParserScreen(Graphics g){

        int w = width;
        int h = fontHeight+2;
        int x = 0;
        int y = height - h;

        int curLoaded = 0;
        int value = 0;
        int iPercent = 0;



        if(maxElementNum!=0){
            curLoaded = wapRender.getCurLoadedTag();
            value = curLoaded * 100 / maxElementNum;
            iPercent = (curLoaded * (w - 2)) / maxElementNum;
        }

        g.setColor(0x808080);
        g.fillRect(x, y, w, h);

        g.setColor(0x0000ff);
        g.fillRect(x + 1, y + 1, iPercent - 2, h - 1);

        g.setColor(0xffffff);
        g.drawString("proses..." + value+"%", 
                width>>1, y + 1, Graphics.TOP|Graphics.HCENTER);
    }

and they said

java.lang.SecurityException: MIDP lifecycle does not support system exit.
    at java.lang.Runtime.exit(+9)
    at java.lang.System.exit(+7)
    at com.gameislive.browser.Browser.stop(+8)
    at Tampilkan.commandAction(+147)
    at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+282)
    at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(+10)
    at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(+68)
    at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(+47)
    at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+250)

please help me what should i do for this case?

标签: java-me
2条回答
走好不送
2楼-- · 2019-04-13 05:09

Try calling norifyDestroyed() method in MIDlet instance.

查看更多
闹够了就滚
3楼-- · 2019-04-13 05:23

The security exception you get says it all.

J2ME applications do NOT behave like J2SE applications.

You don't start them in the same way and you don't terminate them in the same way.

In your case, the kind of J2ME application you are trying to write is called a MIDlet.

A MIDlet lifecycle is regulated by the MIDP runtime that runs on top of a Java Virtual Machine that simply executes Java bytecode and handles system resources.

When a MIDlet starts, the MIDP runtime calls the MIDlet constructor and its javax.microedition.midlet.MIDlet.startApp() override.

In order to terminate a MIDlet, the MIDP runtime calls the javax.microedition.midlet.MIDlet.destroyApp() override.

When the MIDlet decides that it can be terminated, it can call its own destroyApp() instead of waiting for the MIDP runtime to do it.

In order to tell the MIDP runtime that it can be safely terminated, a MIDlet MUST call javax.microedition.midlet.MIDlet.notifyDestroyed(), typically as the last method call in destroyApp()

I suggest reading the MIDP specifications in order to understand all the lifecycle and runtime issues.

The latest JavaME SDK also contains many properly constructed MIDlets for your inspiration.

查看更多
登录 后发表回答