Detecting network connection for your AIR application
I was looking at the internet for the simple solution to see if my air app has connection with the internet.
I found a lot of blogs, all telling me a little more, but no direct good demo.
This is the source I use while I work with AIR beta 3.
CODE:
-
[XML]
-
-
<mx:windowedapplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" applicationcomplete="init()" title="Network Change Event Log">
-
<mx:script>
-
<!--[CDATA[
-
import flash.events.NetStatusEvent;
-
import flash.net.URLRequest;
-
-
public function init():void{
-
NativeApplication.nativeApplication.addEventListener(Event.NETWORK_CHANGE,onNetworkChange);
-
}
-
-
public function onNetworkChange(networkChangeEvent:Event):void{
-
var now:String = new Date().toTimeString();
-
logEvent("Network change event at: " + now);
-
ping();
-
}
-
-
public function ping():void{
-
var headRequest:URLRequest = new URLRequest();
-
headRequest.method = "HEAD";
-
headRequest.url = resourceURL.text;
-
-
logEvent("Requesting: " + headRequest.url.toString());
-
var response:URLLoader = new URLLoader(headRequest);
-
response.addEventListener(HTTPStatusEvent.HTTP_STATUS,onHTTPStatus);
-
response.addEventListener(Event.COMPLETE,onComplete);
-
response.addEventListener(IOErrorEvent.IO_ERROR,onError);
-
response.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSecurityError);
-
}
-
-
public function onComplete(status:Event):void{
-
logEvent(" Response received.");
-
}
-
-
public function onHTTPStatus(status:HTTPStatusEvent):void{
-
logEvent(" Status: " + status.status);
-
}
-
-
public function onError(error:IOErrorEvent):void{
-
logEvent(" Request failed. " + error.text);
-
}
-
-
public function onSecurityError(error:SecurityErrorEvent):void{
-
logEvent(" Request failed. " + error.text);
-
}
-
-
public function logEvent(entry:String):void {
-
log.text += entry + "\n";
-
trace(entry);
-
}
-
]]-->
-
</mx:script>
-
<mx:hbox width="100%">
-
<mx:button label="Ping" id="pingButton" click="ping();" width="30%">
-
<mx:textinput id="resourceURL" text="http://www.wowww.nl" width="70%">
-
</mx:textinput>
-
<mx:textarea id="log" width="100%" height="100%" editable="false">
-
valueCommit="log.verticalScrollPosition=log.textHeight;"/>
-
</mx:textarea>
-
[/XML]
Gepost in categorie: Social
Tweet
Reacties
Plaats reactie zonder facebook account