Search

 

February 2012
S M T W T F S
« Nov    
 1234
567891011
12131415161718
19202122232425
26272829  

Tags

Archives

ConnectionManager

By Rich Tretola | February 13, 2008
4,144 views

Many AIR applications require Internet access. The problem is that since the application is launched from the desktop, an Internet connection can not be assumed. I have created a ConnectionManager that will handle all that is involved in testing for a connection.

Basic sample initializing the ConnectionManager:

1
private var cm:ConnectionManager = new ConnectionManager();

The constructor of the ConnectionManager class can also accept up to four optional arguments.

Constructor arguments
showMessage:Boolean – if true, the user will be alerted when a connection failure occurs. defaults to true
connectionURL:String – the URL used to test for an Internet connection. defaults to http://www.google.com
message:String – the message that will be displayed to the user when an Internet connection test fails. defaults to “This application requires\nan Internet connection”
alertType:String – the type of alert, can be either “popup” which will display a mx.controls.Alert window or “toast” which will utilize the AlertWindow component preset to be a critical alert. defaults to popup

properties
isConnected – bindable property which holds the current connection status

The first example uses the ConnectionManager with all of the default values. Upon load of the application, the ConnectionManager will attempt to connect to http://www.google.com and alert the user with a popup alert if a connection failure occurs. The isConnection property is also being shown.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="absolute">
    <mx:Script>
        <![CDATA[
            import com.everythingflex.air.managers.ConnectionManager;
            [Bindable]
            private var cm:ConnectionManager = new ConnectionManager();

        ]]>
    </mx:Script>
    <mx:Label text="Connection Status: {cm.isConnected}"
        fontSize="20"
        horizontalCenter="0"
        verticalCenter="0"/>
</mx:WindowedApplication>

eflex006.jpg

The second example customizes the ConnectionManager by defining a connectionURL, message, and also setting the alertType to toast. Upon launch of the application, the ConnectionManager will attempt to connect to http://www.adobe.com and show the message “Please check your Internet connection” in an AlertWindow when a connection failure occurs. The isConnection property is also being shown.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="absolute">
    <mx:Script>
        <![CDATA[
            import com.everythingflex.air.managers.ConnectionManager;

            [Bindable]
            private var cm:ConnectionManager = new ConnectionManager(true,
                                            "http://adobe.com",
                                            "Please check your Internet connection",
                                            "toast");
        ]]>
    </mx:Script>
    <mx:Label text="Connection Status: {cm.isConnected}"
        fontSize="20"
        horizontalCenter="0"
        verticalCenter="0"/>
</mx:WindowedApplication>

eflex007.jpg