udit bhardwaj
udit bhardwaj

Reputation: 71

How to check whether an internet/network connection available or not , but only for once

How Can I check if an internet connection is available or not but Only For ONCE (One Time) . Remember One Time Execution Only

I am using the following code but it is continuously monitoring the availability of the internet connection. I want to run it (execute) it only once. How can I do it or there is a way to check for a function that it has been executed or not with the help of boolean type Variable?

I have tried the loop for(i=0 to i = 1) but it makes my application crash every time.

private function init():void
{
    NativeApplication.nativeApplication.addEventListener(Event.NETWORK_CHANGE, onNetworkChange);
    monitorConnection();
}

private function onNetworkChange(event:Event):void
{
    trace('network change');
    monitorConnection();
}

private function monitorConnection():void
{
    monitor = new URLMonitor(new URLRequest(strURLMonitor));
    monitor.addEventListener(StatusEvent.STATUS,announceStatus);
    monitor.start();
}

private function announceStatus(e:StatusEvent):void {
    trace("Status change. Current status: " + monitor.available);
    if(monitor.available)
    {
        // Do something
    } else {
        // Do something else
    }
}

Any Help will be greatly Admired

Thank You Udit Bhardwaj

Upvotes: 0

Views: 1016

Answers (2)

Adrian Pirvulescu
Adrian Pirvulescu

Reputation: 4340

Just do... "monitor.removeEventListener(StatusEvent.STATUS,announceStatus);" and call only once monitorConnection();

private function monitorConnection():void
{
    monitor = new URLMonitor(new URLRequest(strURLMonitor));
    monitor.addEventListener(StatusEvent.STATUS,announceStatus);
    monitor.start();
}

private function announceStatus(e:StatusEvent):void {
    monitor.removeEventListener(StatusEvent.STATUS,announceStatus);
    trace("Status change. Current status: " + monitor.available);
    if(monitor.available)
    {
        // Do something
    } else {
        // Do something else
    }
}

Upvotes: -1

Andrey Popov
Andrey Popov

Reputation: 7510

Best practice would be to ping some server that's always online - like Google for example :) It's easy and fast. The URLMonitor is for monitoring, not for test.. And there is no built-in function just for single test.

Use simple URLLoader :)

Upvotes: 3

Related Questions