Adobe Air - 检查网络连接

3

我正在使用Adobe Flash CS 5构建一个Air应用程序。我需要检查是否有可用的互联网连接。

但是我遇到了以下错误:

1172: 找不到 air.net 的定义。 1046: 类型未找到或不是编译时常量: URLMonitor。

这是我的代码:

import air.net.*;

var monitor:URLMonitor;

function checkInternetConnection(e:Event = null):void
{
var url:URLRequest = new URLRequest("http://www.google.com");
url.method = "HEAD";
monitor = new URLMonitor(url);
monitor.pollInterval = 3000;
//
monitor.addEventListener(StatusEvent.STATUS,onConnection);
//
function onConnection(e:Event = null):void
{
trace("onConnection")
}
//
monitor.start();
trace(monitor)
} 

什么缺失了? 谢谢。 Uli
5个回答

10

您好,我已经成功使用了以下代码。

您只需要导入:import air.net.URLMonitor;

protected function init():void
        {
            // Center main AIR app window on the screen
            nativeWindow.x = (Capabilities.screenResolutionX - nativeWindow.width) / 2;
            nativeWindow.y = (Capabilities.screenResolutionY - nativeWindow.height) / 2;
            // Detects a general change in network status
            NativeApplication.nativeApplication.addEventListener(Event.NETWORK_CHANGE,onNetworkChange);
        }

        //Checking for network connectivity
        protected function onNetworkChange(e:Event):void
        {
        //  Alert.show("Your Network State changed", "INFO");
            monitor = new URLMonitor(new URLRequest('http://www.adobe.com'));
            monitor.addEventListener(StatusEvent.STATUS, netConnectivity);
            monitor.start();
        }

        protected function netConnectivity(e:StatusEvent):void 
        {
            if(monitor.available)
            {
                Alert.show("Status change. You are connected to the internet", "INFO");
            }
            else
            {
                Alert.show("Status change. You are not connected to the internet", "INFO");
            }

            monitor.stop();
        }

7

您需要添加aircore.swc

进入 文件-> ActionScript 设置

在“库路径”选项卡上,点击“+”图标(添加新路径),然后点击 Flash 图标(浏览到 SWC)。

接下来,您需要浏览到Flash CS5安装的位置,然后转到AIK2.5/frameworks/libs/air/并选择aircore.swc


已经完成了,但现在我遇到了这个错误:5000:类“air.net.URLMonitor”必须继承“flash.display.MovieClip”,因为它链接到该类型的库符号。 我从组件检查器中将URLMonitor组件拖到了库中。 - Uli
@Uli,你是否按照上面的步骤进行操作了?我不确定为什么你要将任何组件拖入库中。 - Pixel Elephant


1
{Flash_CS5_installation_path}/AIR{air_version}/frameworks/libs/air 目录下,您可以找到 *.swc 文件,这些文件需要包含在您的应用程序中。关于“如何包含”,请阅读此处

0
你的应用程序中是否有这些类可用:
import air.net.*;
即一个名为“air”的目录与您的.fla文件并列。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接