AIR has made it very easy to test for Internet connections using the Event.NETWORK_CHANGE event, which is an event broadcast by the main Shell class.
To demonstrate how to test for network connections, create a new AIR project named Chapter13_ConnStatus. You should now have an empty file named Chapter13_ConnStatus.mxml. Set the title to Connection Status and the width to 400. Your file should now look like Listing 13-1.
<?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" title="Connection Status" width="400"> </mx:WindowedApplication>
The next thing we're going to need to add to the Chapter13_ConnStatus.mxml file is a block of import statements to bring in the URLMonitor, URLRequest, and StatusEvent classes. Create a new <mx:Script> block in this file, and add the contents of Listing 13-2.
import flash.events.StatusEvent; import flash.net.URLRequest; import air.net.URLMonitor; ]]> </mx:Script>
Now we need to add a variable to hold the current connection status, which is set to False by default. We also need to create a variable of type URLMonitor, which needs to be a class variable so that the events can be announced. Add the following right below the import statements:
[Bindable] private var isConnected:Boolean = false ...