AIR is meant to facilitate applications that work when online and offline. For example, if you developed an AIR application to manage your blog, you would want to be able to write blog posts whether you were online or offline. To accomplish this, the application would do one of two actions depending on whether it had an internet connection. If it were online, it would take your post and upload it. If it weren't online, it would store it (either in a file or in a local SQLite database).
VERSION: This tutorial is current for AIR Beta 3.
Today, you are going to build a very simple AIR application. If will be a window that will have three main items: a search box, a submit button, and an image that indicates if you are connected to the Internet. If a user types something into the text input and hits the search button, it will open up your browser and search for that term on Google. However, if you are not connected to the Internet, the "search" button will be disabled.

AIR has two specific functions for monitoring your internet connection, URLMonitor [ Flex | Javascript ] and SocketMonitor [ Flex | Javascript ]. These classes both implement the ServiceMonitor Class [ Flex | Javascript ]. To monitor your connection you just follow the steps below:
If any of this seems confusing, don't worry, you will look at each item in the code below. Also, the full source code will be available for both examples.
Coding the Example
For the application, you are going to create a function that gets run when the application starts. This will be where you will create your URLRequest and URLMonitor objects. For Flex, this will be a function that responds to the CreationComplete event, for the HTML/Javascript example, this will be a function that responds to the "onload" event of the body tag.
Flex
HTML/Javascript
Hopefully you can see that there is not a great deal of difference between the two. Calling an AIR function within Javascript is just as easy as it is in Flex.
NOTE: If you want this javascript to function properly, be sure to include the AIRAliases.js file and the servicemonitor.swf file in your application. If you do not, this code will not function properly. Your code can function without the AIRAliases.js - the method names are just longer. However, you cannot use the URLMonitor or SocketMonitor without the servicemonitor.swf file.
Flex Application
Source Code
HTML / Javascript Application
Source Code
I am trying to run this app and I am getting this error
SyntaxError: Parse error
app-resource:/servicemonitor.swf : 1
TypeError: Value (result of expression window.runtime.air.net.URLMonitor) is not a constructor. Cannot be used with new.
onLoad at app-resource:/tip.html : 21
onLoad at app-resource:/tip.html : 21
onload at app-resource:/tip.html : 72
I haven’t updated this tutorial for AIR Bet 2 yet. I am not sure if this is the cause of that issue – but I plan to update it within the next day or two.
I don’t know if you are aware of it, but there’s an huge amount of spam links in your article source, hidden in a display: none bold tag….
@Ruben – Thanks! I am still recovering from a minor hack a while back.
[...] AIR技巧 AIR Tip 1: Monitoring a Network Connection (AIR Beta 3) AIR Tip 2: Going Fullscreen (AIR Beta 3) AIR Tip 3: What Version is My Application [...]
[...] AIR Tip 1: Monitoring a Network Connection (AIR 1.0) [...]
[...] 原文在这里 [...]
Also worth mentioning is that if you want to do this using Flash instead of Flex or Javascript then you will need to drag a copy of the ‘ServiceMonitorShim’ component (found in the Components panel under AIR ServiceMonitor) in to your Library.
If you don’t do this you will see some/all of the following compiler messages …
1046: Type was not found or was not a compile-time constant: URLMonitor.
1180: Call to a possibly undefined method URLMonitor.
1046: Type was not found or was not a compile-time constant: URLMonitor.
1120: Access of undefined property URLMonitor.
1172: Definition air.net:URLMonitor could not be found.
This can easily catch people out (including myself).
Hope this helps someone.
Regards,
Adrian
In case anyone ever gets the first commenter’s issues, you must make sure to include the ServiceMonitor library (either in Flex, Flash, or HTML … which all have differing methods of inclusion), and also make sure it’s case-sensitive. I had a lowercase filename (in Windows) and it would keep throwing the error until I fixed the case sensitivity of the file in my application.
Hi,
came across this example.
i am a noob to flex / AIR .
Is it possible to implement this example inside flex app(not AIR) , to check the connection ? If yes, do urlMonitor and SocketMonitor have a counterpart in flex ?
HttpRequest and other URLLoader functions are not compatible with various browsers and hence not reliable .
The goal is to simulate connection monitoring inside a flex app (swf)
Thanks Adrian, you really help me.
[...] of methods to detect network connectivity, all of which can be read about here, here, here and here. What I found is that these seem to work fine when there is an actual change in your network [...]
Hi ,
I cant not get this code to work for adobe air 2 beta 2
can somebody please help ?
i m getting the error : TypeError: Result of expression ‘air.URLMonitor’ [] is not a constructor.
David any help please ?
hi,
it works but it’s far from real time. It takes very long to detect that a network connection is dead but it’s very quick to detec it’s up again. I thougth about idleTimeout but it does not work. Any idea ?
Thank you for your precious advice David, I forget to use “PollInterval”, but its ok now thanks to you