Bill
Bill

Reputation: 19238

Easiest method to check network status on iPad with Adobe Air

I would like to check the network status on the iPad with Adobe Air, at this moment i am checking by sending a URLREQUEST to Google.com. Is there any other better way to monitor the network status, and know if there is a connection or connection have been dropped. Thanks for any suggestion.

Upvotes: 2

Views: 2324

Answers (1)

Raja Jaganathan
Raja Jaganathan

Reputation: 36107

Tricky part here we are not ping some interval times. so we use

NativeApplication.nativeApplication.addEventListener(Event.NETWORK_CHANGE, onNetwork_ChangeHandler);

It will trigger when ever change net status change so performance wise also good.

Because Ping is very costly operation.So call start() URLMontior after network changed triggered then we call stop().

      package com.abc.net
    {
    import air.net.URLMonitor;
    import flash.desktop.NativeApplication;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.IEventDispatcher;
    import flash.events.StatusEvent;
    import flash.net.URLRequest;

    [Event(name="networkStatusChanged", type="flash.events.Event")]
    public class NetStatusMonitor extends EventDispatcher
    {
        private var urlMonitor:URLMonitor;
        private var url:String;

        public function KBNetStatusMonitor(url:String = 'http://www.adobe.com')
        {
            super();
            this.url = url;
            NativeApplication.nativeApplication.addEventListener(Event.NETWORK_CHANGE, onNetwork_ChangeHandler);
        }

        protected function onNetwork_ChangeHandler(event:Event):void
        {
            start();
        }       

        public function start():void
        {
            urlMonitor = new URLMonitor(new URLRequest(url));
            urlMonitor.addEventListener(StatusEvent.STATUS, onNetStatus_ChangeHandler);

            if(!urlMonitor.running)
                urlMonitor.start();
        }

        public function stop():void
        {
            if(urlMonitor.running)
                urlMonitor.stop();
        }

        private function onNetStatus_ChangeHandler(event:StatusEvent):void
        {
            trace("event code " + event.code);
            trace("event level " + event.level);
            dispatchEvent(new NetStatusEvent(NetStatusEvent.NETWORK_STATUS_CHANGED,urlMonitor.available));
            stop();
        }
    }
}

Event Class

package com.abc.net
{
    import flash.events.Event;

    public class NetStatusEvent extends Event
    {
        public static const  NETWORK_STATUS_CHANGED:String = "networkStatusChanged";

        public var status:Boolean;

        public function NetStatusEvent(type:String, status:Boolean, bubbles:Boolean=false, cancelable:Boolean=false)
        {
            super(type, bubbles, cancelable);
            this.status = status;
        }

        override public function clone():Event
        {
            return new NetStatusEvent(type, status, bubbles,cancelable);
        }

    }
}

Usage :

var netStatusMonitor:NetStatusMonitor = new NetStatusMonitor();
    netStatusMonitor.addEventListener(NetStatusEvent.NETWORK_STATUS_CHANGED,function(event:NetStatusEvent):void {
           statusCallBack.call(null,event.status);
});

netStatusMonitor.start();

Upvotes: 6

Related Questions