2

有没有办法在应用程序关闭之前执行某些操作?
例如,有人关闭了应用程序,但在它发生之前我必须向服务器发送一些数据......

4

1 回答 1

2

请说明是哪个平台!!!

对于浏览器,您必须使用 JavaScript onbeforeunload 事件和 Flash externalInterface。这是一个如何链接!

对于android,它看起来像这样!

if(Capabilities.cpuArchitecture=="ARM") // check if Android
{
    NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, handleActivate, false, 0, true);
    NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, handleDeactivate, false, 0, true);
    NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, handleKeys, false, 0, true);
}

private function handleActivate(event:Event):void
{
    // load your stuff again or whatever 
    NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
}

private function handleDeactivate(event:Event):void
{
    // Save your Stuff and do whatever!!!
    NativeApplication.nativeApplication.exit();
}

private function handleKeys(event:KeyboardEvent):void
{
    if(event.keyCode == Keyboard.BACK){
        // Save your Stuff and do whatever!!!
        NativeApplication.nativeApplication.exit();
    }
}
于 2013-02-28T19:14:58.007 回答