public static var iStatus:Boolean;
public static function initMonitor():void
{
var url:URLRequest = new URLRequest('http://www.nature.com') ;
url.method = "HEAD";
_monitor = new URLMonitor(url);
_monitor.addEventListener(StatusEvent.STATUS, announceStatus);
_monitor.pollInterval = 1000;
_monitor.start();
}
public static function announceStatus(event:StatusEvent):void
{
if(_monitor.available)
{
iStatus=true;
}
else
{
iStatus=false;
}
}
问题是,当我最初运行没有互联网连接的应用时,它会返回状态false
。如果我打开互联网连接,它将返回状态true
。但是,如果我再次关闭互联网连接,它将不会返回false
的状态。此时,它没有输入announceStatus
方法。
我认为每次互联网连接打开或关闭时都会调用announceStatus
,但它不会像那样工作。任何建议都会有所帮助。
答案 0 :(得分:0)
public static var iStatus:Boolean;
public static function initMonitor():void
{
NativeApplication.nativeApplication.addEventListener(Event.NETWORK_CHANGE, onNetworkChange);
onNetworkChange();
}
protected function onNetworkChange(e:Event):void
{
_monitor = new URLMonitor(new URLRequest('http://www.nature.com'));
_monitor.addEventListener(StatusEvent.STATUS, announceStatus);
_monitor.pollInterval = 1000;
_monitor.start();
}
public static function announceStatus(event:StatusEvent):void
{
if(_monitor.available)
{
iStatus=true;
}
else
{
iStatus=false;
}
}