Loading Video...
Hide Video Detect network connectivity with Flex / AIR applications

Detecting Network Connectivity in Flex AIR applications

Play Video

Detecting network connectivity is a crucial part of AIR application development.

If you’re releasing or developing AIR applications that require an internet connection, you want to ensure that you have the connectivity before you start running your ‘online code’ to trap any possible errors that may occur from lack of connection or downtime.

In this brief video tutorial, I outline the basics required to set up network connectivity monitoring functionality inside of your AIR application.

To view the video, click on the video link above, or watch it directly on YouTube.

MXML Code

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
					   xmlns:s="library://ns.adobe.com/flex/spark" 
					   xmlns:mx="library://ns.adobe.com/flex/mx" 
					   creationComplete="init()">

	<fx:Script>
		<![CDATA[
			// network monitoring functions
			/* **************************** */

			// import the URLMonitor class
			import air.net.URLMonitor;

			/*
			Set up the URL string variable for the URLRequest to check.
			In this example, let's use google.com as we can be sure 99.99%
			of the time that it will be up and running.
			*/
			private var strURLMonitor	: String = 'http://www.google.com';
			private var	monitor			: URLMonitor;
			[Bindable] 
			private var isOnline		: Boolean = false;

			/*
			This function checks the online status by attempting 
			to resolve a connection to a remote address
			*/
			private function monitorConnection():void {
				monitor = new URLMonitor(
								new URLRequest(strURLMonitor)
							);
				monitor.addEventListener(
								StatusEvent.STATUS, 
								announceStatus);
				monitor.start();
			}

			/*
			Declare the status from the monitorConnection function
			and set the value to the variable isOnline
			*/
			private function announceStatus(e:StatusEvent):void {
				trace("Status change. Current status: " + monitor.available);
				if(monitor.available) {
					isOnline = true;
					/*
					Inside here, you can run any functions or script
					that require network connection, as it exists.
					*/
				} else {
					isOnline = false;
				}
			}

			/* 
			The network connection has changed.
			Run the monitorConnection function to check status
			*/
			private function onNetworkChange(event:Event):void {
				trace('network change');
				monitorConnection();
			}
			//

			private function init():void {
				/* 
					Keep an eye on network connectivity... 
					Any losses, and run the onNetworkChange method
				*/
				NativeApplication.nativeApplication.addEventListener(
						Event.NETWORK_CHANGE, onNetworkChange);

				// run monitorConnection
				monitorConnection();
			}

		]]>
	</fx:Script>

	<s:Label x="10" 
			 y="10" 
			 text="Online? {isOnline}" 
			 width="493" 
			 height="167" 
			 fontSize="72" />

</s:WindowedApplication>
  • Thiru

    Hi the video and the code was really help full, I am new to AIR application development, Thanks

  • Veyron

    Can you maybe show a sample where an alert box open if there is no internet connection?

  • http://www.mattgifford.co.uk/ Matt

    Hi Veyron

    To add a simple rudimentary alert box, you would need to add the following code to import that alert features:

    import mx.controls.Alert;

    and the revise the announceStatus() method to add the following within the offline exception:

    } else {
    isOnline = false;
    /* new code added here for Alert box */
    Alert.show(
    ‘The application has been unable to detect any ‘ +
    ‘network connectivity and appears to be offline.’,
    ‘Offline Notification’
    );
    }

    Does that help?

  • Veyron

    100%

    Thanks!

  • Veyron

    A quick question, is there any way to add this code:

    inside the fx:Script?

    • http://www.mattgifford.co.uk/ Matt

      Glad that first comment helped.

      The code didnt render within your second comment though. You can send it again and try to escape the tag characters, or email me directly from the contact form.

  • Abin

    But this piece off code is creating sessions and not clearing off doze
    and so creatin probz…. how will i handle diz scenario…..

    thankz in advance

    waitin for some quick repli :)

  • http://www.deepvoid.it Alex

    The video is gone, can you make it available again? Perhaps using Youtube? Thanks!!! :-)

    • http://www.mattgifford.co.uk/ Matt

      Hi Alex

      Thanks for the heads-up about the video. Fixed and back up and running now.

      • http://www.deepvoid.it Alex

        Fantastic! I really thank you for your help!!! :-)

      • http://www.deepvoid.it Alex

        Great tutorial, nice and easy! :-)

        I’m trying to “enlarge” it by adding some features, such as speed meter, logging, and so on…

        • http://www.mattgifford.co.uk/ Matt

          Hey Alex. Glad it helped you out! I look forward to seeing your extended version of the code :)