【问题标题】:What is the preferred way to pause an AIR-for-mobile app?暂停 AIR-for-mobile 应用程序的首选方式是什么?
【发布时间】:2013-02-08 00:30:01
【问题描述】:

我制作了一款 Flash Air 游戏。在玩游戏时,如果我按下 Android 设备上的 Home 按钮,我会进入手机菜单,但我仍然可以听游戏的音乐,这意味着游戏仍然可以正常运行。

我希望我的应用在不在前台时暂停。我想到了这个:

...

        stage.addEventListener(flash.events.Event.DEACTIVATE, deactivated);

...

    private function deactivated(e:flash.events.Event):void {
        delayedCall = new DelayedCall(quitNow, 2 * 60);
        stage.addEventListener(flash.events.Event.ACTIVATE, reactivated);
    }
    private function quitNow():void {
        NativeApplication.nativeApplication.exit();
    }
    private function reactivated(e:flash.events.Event):void {
        stage.removeEventListener(flash.events.Event.ACTIVATE, reactivated);
        if (delayedCall) {
            delayedCall.dismiss();
            delayedCall = null;
        }
    }

...

DelayedCall 是一个自定义的“调用此函数前等待”类:

package fanlib.utils {
import flash.utils.Timer;
import flash.events.TimerEvent;

public class DelayedCall {

    private var data:*;
    private var callback:Function;
    private var timer:Timer;

    public function DelayedCall(callb:Function, seconds:Number, dat:* = undefined, count:int = 1) {
        callback = callb;
        data = dat;

        timer = new Timer(seconds * 1000, count);
        timer.addEventListener(TimerEvent.TIMER, timeEvent);
        timer.start();
    }

    public function dismiss():void {
        timer.stop();
        complete();
    }

    private function timeEvent(e:TimerEvent):void {
        if (data) callback(data); else callback();
        if (timer.currentCount == timer.repeatCount) complete();
    }

    private function complete():void {
        timer = null;
        data = undefined;
    }
}
}

上述方法适用于 Air Desktop 版本。当窗口失焦时,应用会在 2*60 秒后退出。

在 Android 上,这种行为非常特殊:

  • delayCall 永远不会调用quitNow()
  • 如果我删除“stage.addEventListener(flash.events.Event.ACTIVATE, reactivated);”,如果我在 2*60 秒后返回它,应用程序将关闭,但如果我不这样做,它不会关闭不要回头!!!

这似乎是当应用程序不在前台时,Actionscript 代码确实会暂停,但音乐仍然在播放。

有任何想法或更好的、替代的、直接证明的方法吗?我不打算深入研究 Air 和 Android 的内部工作原理......

【问题讨论】:

    标签: flash mobile air


    【解决方案1】:

    在 Android 上,您可以收听 NativeApplication 的 DEACTIVATE 和 ACTIVATE 事件。当应用程序被发送到后台时,handleApplicationDeactivated 会被调用。当它返回时 handleApplicationActivated 被调用。希望这会有所帮助。

    NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, handleApplicationDeactivated);
    NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, handleApplicationActivated);
    

    【讨论】:

      猜你喜欢
      • 2011-08-09
      • 1970-01-01
      • 1970-01-01
      • 2011-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-24
      • 1970-01-01
      相关资源
      最近更新 更多