-1

Please i am new to Flex and Actionscript 3, but i know its an OOP language, I'm coming from a Java Background. I have a class that helps me check if the user of my AIR app has an internet connection it works well when included directly in my mxml class. Wanted to know if its possible to insert it in an actionscript class and reuse it in any mxml component class i wish to.

EDIT

package components
{
    import air.net.URLMonitor;

    import flash.events.StatusEvent;
    import flash.net.*;

    public class NetworkChecker
    {
        private var monitor:URLMonitor;
        private var myURL = new URLRequest("http://www.adobe.com");

        public function NetworkChecker()
        {
            myURL.method = "HEAD";
            monitor = new URLMonitor(myURL);
            monitor.start();
            monitor.addEventListener(StatusEvent.STATUS, on_Connection);
        }
        public function on_Connection(event:StatusEvent):void
        {
            if(event.target.available == true)
            {
                trace("Internet Connection is available");
            }
            else
            {
                trace("No internet Connection");
            }
        }
    }
}

Please how do i call this code from an mxml component? it works well when i include it directly in the fx:Script tag. I need to know if event.target.available is true of false in my mxml component...

ethrbunny
  • 10,379
  • 9
  • 69
  • 131

2 Answers2

0

Change your function to:

    public function on_Connection(event:StatusEvent):boolean
    {
        if(event.target.available == true)
        {
            trace("Internet Connection is available");
            return true;
        }
        else
        {
            trace("No internet Connection");
            return false;
        }
    }

or better still:

    public function on_Connection(event:StatusEvent):boolean
    {
        return event.target.available;
    }

NOTE: this assumes that event.target.available is defined...

ethrbunny
  • 10,379
  • 9
  • 69
  • 131
0

Add public proprerty to NetworkChecker class

[Bindable]
public var available:Boolean = false;

then

public function on_Connection(event:StatusEvent):void
        {           
           this.available = event.target.available;
        }

mxml

<fx:Declarations>
    <components:NetworkChecker id="checker"/>
</fx:Declarations>

<s:Button label="Button" enabled="{checker.available}"/>

and more:
You should add eventListener before monitor start in constructor function

Y Borys
  • 543
  • 1
  • 5
  • 21