6 Adobe AIR ActionScript APIs explored part II: Network Detection
As previously mentioned the “Developing AIR Applications with Adobe Flex 3” online manual is really a great place to get information for many of the aspects of AIR development using Flex 3.0. This example was created using the code and information available on this page.
There are three classes available to monitor network connectivity for Adobe AIR applciations. These classes are:
ServiceMonitor
SocketMonitor
URLMonitor
This article uses a very simple code example to demonstrate the usage of the URLMonitor class.
To get started you will need to import the URLMonitor class. The URLMonitor class is located in the air.net package.
The next step is to create an initialization method. In this method an instance of the URLMonitor class is created and assigned to the local variable “urlMonitor”.
The URLMonitor constructor accepts an instance of the URLRequest class as an argument. A URLRequest instance named “urlRequest” is created and assigned a value of “http://seantheflexguy.com”. The URLRequest instance is passed into the URLMonitor constructor.
An event listener also needs to be created and registered with the URLMonitor instance. The “onStatusEvent” method is registered to listen for the StatusEvent.STATUS event of our URLMonitor instance.
Finally the “start” method MUST be called to activate the URLMonitor instance.
The “onStatusEvent” method will receive a StatusEvent event object as a parameter. This parameter will contain useful data related to the status of the targeted URLRequest. In particular the “code” property can be used to determine the network status. I was able to get the “Service.available” and the “Service.unavailable” values by disconnecting the RJ45 cable from my laptop with the AIR application running in debug mode in the Flex Builder 3.0 IDE. You can see the trace statements as they were displayed while I ran the app in the screen capture included with this article.
The URLMonitor class enables you to determine if the availability of a network resource is changing. Monitoring one single resource is not a reliable way of determining if application is truly “offline”. One idea toward creating a more reliable online detection script might be to check a collection of five highly visited websites. Then depending on the overall availability of those websites a solid online/offline assessment could be made. If you are looking to monitor a single network resource, such as your server, then the URLMonitor class can be used as-is.
I hope you have enjoyed this article. The next article in this series will explore the notification features of Adobe AIR.
Code Example:
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
creationComplete="init();">
<mx:Script>
<![CDATA[
import air.net.ServiceMonitor;
import air.net.URLMonitor;
private function init() : void
{
var urlMonitor : URLMonitor;
var urlRequest : URLRequest = new URLRequest( "http://seantheflexguy.com" );
urlMonitor = new URLMonitor( urlRequest );
urlMonitor.addEventListener( StatusEvent.STATUS, onStatusEvent );
urlMonitor.start();
}
private function onStatusEvent( event : StatusEvent ) : void
{
var evt : StatusEvent = event;
trace( ">>onStatusEvent::evt.code=" + evt.code );
}
]]>
</mx:Script>
</mx:WindowedApplication>


Great Post, Sean… will have to check this out more in-depth when I have a moment. Saw those three classes the other after dl’ing Air SDK 1.0… wanted to play with them.
Thanks!
Can the ServiceMonitor or any of these classes can monitor AMF data?
Very interesting post. Thanks for sharing!
This is of great help Sean..
I have a question can we use develop an application which would include AIR packages in it. I read somewhere “it is not advisable to include AIR packages in application if it is to be executed on both platforms(Flex & AIR).
Can you put some light on this,it would be of great help.
Thanks and regards,
Jigar
Hi Sean
Is there a way in Flex that i can capture the desktop activities & stream it to FMS where it can be broadcasted to different clients connected.
Thanks
Hi Sean,
I have been looking for the same answers Piyush is seeking. Other blogs have cited security concerns, however for an app that is given permission to write/erase local files as well as upload those files to a remote server, I can’t see where capturing the desktop and uploading the captures to an FMS server would be outside the realm of acceptability on Adobe’s behalf.
Thanks
[...] 6 Adobe AIR ActionScript APIs explored part II: Network Detection - seantheflashguy.com [...]
[...] Read more @ http://seantheflashguy.com/blog/2008/03/04/6-adobe-air-actionscript-apis-explored-part-ii-network-de... [...]