Adobe Air – Connection Manager
Here is a very useful script that I re-use and re-use when building Air applications. Its a simple script I am sure you can make out what is going on here.
ConnectionManager.as
package com.jonniespratley.air.managers
{
import air.net.URLMonitor;
import flash.events.StatusEvent;
import flash.net.URLRequest;
import mx.controls.Alert;
public class ConnectionManager
{
private var statusEvent:StatusEvent;
private var urlMonitor:URLMonitor;
private var showAlert:Boolean;
private var message:String;
[Bindable] public var connectionURL:String;
[Bindable] public var isConnected:Boolean;
public function ConnectionManager( showAlert:Boolean = true,
connectionURL:String = "http://del.icio.us",
message:String = "Del.icio.us Air must be connected to the internet to retrieve lastest bookmarks\nan Internet Connection" )
{
this.showAlert = showAlert;
this.connectionURL = connectionURL;
this.message = message;
startMonitor();
}
public function startMonitor():void
{
var urlRequest:URLRequest = new URLRequest( connectionURL );
urlRequest.method = "HEAD";
urlMonitor = new URLMonitor( urlRequest );
urlMonitor.addEventListener( StatusEvent.STATUS, statusChanged );
urlMonitor.start();
}
public function statusChanged( event:StatusEvent ):void
{
this.isConnected = urlMonitor.available;
if ( !this.isConnected && this.showAlert )
{
Alert.show( this.message, "Connection Failure" );
}
statusEvent = new StatusEvent( StatusEvent.STATUS );
dispatchEvent( statusEvent );
}
}
}




October 13th, 2008 at 10:57 pm
I’d go with something simpler, or just quicker to write
var monit:URLMonitor;
function init():void {
var req:URLRequest = new URLRequest(“http://spreadingfunkyness.com”);
req.method = URLRequestMethod.HEAD;
monit = new URLMonitor(req);
monit.addEventListener(StatusEvent.STATUS, change);
monit.start();
}
function change(e:StatusEvent):void {
trace(“status is “+monit.available);
}
December 14th, 2008 at 9:58 pm
Helpful post! Just an fyi – the URLRequest method as “HEAD” fails with a 405 MethodNotAllowed when querying against http://www.amazon.com. Additionally, the URLMonitor can be tricked to think that the endpoint is online if the individual uses a service like OpenDNS – which swallows the error and sends back a page of ‘recommendations’ and a status code of 200. So… rather than thinking that the URLMonitor is a means to determine that you are ‘online’ it really should be used to determine that the endpoints you use within the application are ‘available.’
December 15th, 2008 at 7:52 pm
Thats good to know, I’ll watch for that next time I do anything with amazon. How should we go about doing this is a fault proof solution?
March 3rd, 2009 at 2:47 am
Hello webmaster
I would like to share with you a link to your site
write me here preonrelt@mail.ru
April 26th, 2010 at 8:30 am
I am a Pensioner. Not very computer literate.
I don’t know how or why but Adobe Air tried to install on my laptop.
I don’t recall downloading it.
However, it says it is damaged, so download again.
It always says the same thing.
I have no idea what Air does, and doubt I need it,since I have never had the chance to use it.
Contact Adobe?
You must be joking. After 30 minutes trying I go to lie down with an ice bag on my forehead.
“OK, let’s get rid of the bloody thing”.
You think life is that simple?
It won’t Delete and has now jammed my Add and Remove Page.
Hopefuly it will disappear when I close down.
For sure, I will never download another Adobe product in the future.
I can’t even tell Adobe this. They live in a world of their own.
I don’t appear to be the first who has had problems like this.