【问题标题】:How to check whether an internet/network connection available or not , but only for once如何检查互联网/网络连接是否可用,但仅限一次
【发布时间】:2014-06-02 21:56:29
【问题描述】:

如何检查互联网连接是否可用但仅限一次(一次)只记得一次性执行

我正在使用以下代码,但它会持续监控互联网连接的可用性。 我只想运行(执行)一次。我该怎么做,或者有一种方法可以在布尔类型变量的帮助下检查函数是否已执行

我尝试了 for(i=0 to i = 1) 循环,但它每次都会让我的应用程序崩溃。

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
    }
}

任何帮助都会受到极大的钦佩

谢谢你 乌迪特·巴德瓦杰

【问题讨论】:

    标签: actionscript-3 apache-flex flex4.6


    【解决方案1】:

    最佳做法是 ping 一些始终在线的服务器 - 例如 Google :) 它既简单又快速。 URLMonitor 是用来监控的,不是用来测试的。而且没有内置功能只针对单次测试。

    使用简单的 URLLoader :)

    【讨论】:

    • strURLMOnitor 是一个字符串变量,有值 www.Google.com 所以它会持续监控。我有一个无限循环,比如私有 var strURLMonitor : String = 'google.com';私有变量监视器:URLMonitor;
    • 正如我所说 - 只需使用 URLLoader 并向 Google 发出一个请求。
    • 感谢您的回答,但我为此使用了全局“布尔变量”,条件为“false”,执行后我将其值更改为“True”和 if(IsExecuted == false){进一步执行} else{no execution} 非常感谢您的回答
    【解决方案2】:

    只需...“monitor.removeEventListener(StatusEvent.STATUS,announceStatus);”并且只调用一次 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
        }
    }
    

    【讨论】:

    • 好吧,如果是这样的话,至少使用stop 而不是仅仅删除监听器——这意味着它会继续工作但你不会得到监听器。这意味着完全不需要内存泄漏!
    猜你喜欢
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 2019-09-03
    • 1970-01-01
    相关资源
    最近更新 更多