13.1. Checking for an Internet Connection

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.

Example 13-1. The beginning of the Chapter13_ConnStatus.mxml file
<?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.

Example 13-2. The new Script block with import statements
<mx:Script>
  <![CDATA[
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 ...

Get Beginning Adobe® AIR™: Building Applications for the Adobe Integrated Runtime now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.