Flash ios Apps Cookbook学习整理(一)
1. 设置应用程序休眠状态
通过NativeApplication.nativeApplication可以得到应用程序的实例,这是一个单例模式的引用
设置nativeApplication.systemIdleMode来指定当休眠时间到来时应用程序的状态
systemIdleMode有两个值:SystemIdleMode.NORMAL(默认)与SystemIdleMode.KEEP_AWAKE(保持唤醒)
var application:NativeApplication = NativeApplication.nativeApplication;
application.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
application.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
还有一种灵活的方法,是监听应用程序的Event.ACTIVATE和Event.DEACTIVATE事件,当程序被激活时,设置休眠状态为KEEP_AWAKE(保持唤醒);当程序未激活时(后台运行),设置休眠状态为NORMAL
Code
package
{
import flash.desktop.NativeApplication;
import flash.desktop.SystemIdleMode;
import flash.display.MovieClip;
import flash.events.Event;
public class Main extends MovieClip
{
private var application:NativeApplication;
public function Main()
{
application = NativeApplication.nativeApplication;
application.addEventListener(Event.ACTIVATE,activateHandler);
application.addEventListener(Event.DEACTIVATE,deactivateHandler);
}
private function activateHandler(event:Event):void
{
application.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
}
private function deactivateHandler(event:Event):void
{
application.systemIdleMode = SystemIdleMode.NORMAL;
}
}
}
{
import flash.desktop.NativeApplication;
import flash.desktop.SystemIdleMode;
import flash.display.MovieClip;
import flash.events.Event;
public class Main extends MovieClip
{
private var application:NativeApplication;
public function Main()
{
application = NativeApplication.nativeApplication;
application.addEventListener(Event.ACTIVATE,activateHandler);
application.addEventListener(Event.DEACTIVATE,deactivateHandler);
}
private function activateHandler(event:Event):void
{
application.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
}
private function deactivateHandler(event:Event):void
{
application.systemIdleMode = SystemIdleMode.NORMAL;
}
}
}
2. 应用程序关闭事件
Code
package
{
import flash.desktop.NativeApplication;
import flash.desktop.SystemIdleMode;
import flash.display.MovieClip;
import flash.events.Event;
public class Main extends MovieClip
{
private var application:NativeApplication;
public function Main()
{
application = NativeApplication.nativeApplication;
application.addEventListener(Event.EXITING,exitingHandler);
}
private function exitingHandler(event:Event):void
{
application.removeEventListener(Event.EXITING,exitingHandler);
application.systemIdleMode = SystemIdleMode.NORMAL;
}
}
}
{
import flash.desktop.NativeApplication;
import flash.desktop.SystemIdleMode;
import flash.display.MovieClip;
import flash.events.Event;
public class Main extends MovieClip
{
private var application:NativeApplication;
public function Main()
{
application = NativeApplication.nativeApplication;
application.addEventListener(Event.EXITING,exitingHandler);
}
private function exitingHandler(event:Event):void
{
application.removeEventListener(Event.EXITING,exitingHandler);
application.systemIdleMode = SystemIdleMode.NORMAL;
}
}
}