我在代碼中看到兩個問題。一個是,雖然你有邏輯check internet connection,沒有任何代碼調用該函數,因此重定向的邏輯將不會被調用。其次,使用AIRcore.swc將是一個壞主意,因爲您已經注入了一個可能不適用於或違反瀏覽器沙箱的依賴項。
你可以試試下面的辦法,經過測試,無需AIR的SWC:
步驟1,包括一個新的類ConnectionChecker
如下:
package
{
import flash.events.*;
import flash.net.*;
[Event(name="error", type="flash.events.Event")]
[Event(name="success", type="flash.events.Event")]
public class ConnectionChecker extends EventDispatcher
{
public static const EVENT_SUCCESS:String = "success";
public static const EVENT_ERROR:String = "error";
// Though google.com might be an idea, it is generally a better practice
// to use a url with known content, such as http://foo.com/bar/mytext.txt
// By doing so, known content can also be verified.
// This would make the checking more reliable as the wireless hotspot sign-in
// page would negatively intefere the result.
private var _urlToCheck:String = "http://www.google.com";
// empty string so it would always be postive
private var _contentToCheck:String = "";
public function ConnectionChecker()
{
super();
}
public function check():void
{
var urlRequest:URLRequest = new URLRequest(_urlToCheck);
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, loader_complete);
loader.addEventListener(IOErrorEvent.IO_ERROR, loader_error);
try
{
loader.load(urlRequest);
}
catch (e:Error)
{
dispatchErrorEvent();
}
}
private function loader_complete(event:Event):void
{
var loader:URLLoader = URLLoader(event.target);
var textReceived:String = loader.data as String;
if (textReceived)
{
if (textReceived.indexOf(_contentToCheck))
{
dispatchSuccessEvent();
}
else
{
dispatchErrorEvent();
}
}
else
{
dispatchErrorEvent();
}
}
private function loader_error(event:IOErrorEvent):void
{
dispatchErrorEvent();
}
private function dispatchSuccessEvent():void
{
dispatchEvent(new Event(EVENT_SUCCESS));
}
private function dispatchErrorEvent():void
{
dispatchEvent(new Event(EVENT_ERROR));
}
}
}
第2步,在主應用程序或任何地方互聯網連接應進行檢查,使用下面的代碼片段:
var checker:ConnectionChecker = new ConnectionChecker();
checker.addEventListener(ConnectionChecker.EVENT_SUCCESS, checker_success);
checker.addEventListener(ConnectionChecker.EVENT_ERROR, checker_error);
checker.check();
private function checker_success(event:Event):void
{
// There is internet connection
}
private function checker_error(event:Event):void
{
// There is no internet connection
}
對不起,你介意檢查這個鏈接,請http://stackoverflow.com/questions/39849033/flash-cs6-as3-check-internet-connection-not-working-at-all/39860240?noredirect=1#評論67291875_39860240 –