• AIR检测网络


        package com.juyou.util.net  
        {  
            import flash.events.StatusEvent;  
            import flash.net.URLRequest;  
              
            import air.net.URLMonitor;  
            /** 
             * 网络监听 
             *  
             */  
            public class NetChecker  
            {  
                private static var request:URLRequest = new URLRequest("http://www.baidu.com");  
                private static var monitor:URLMonitor;  
                /**检测间隔时间*/  
                public static var _interval:Number = 10000;  
                private static var available:Boolean = true;  
                private static var observerList:Vector.<INetObserver> = new Vector.<INetObserver>();  
                public function NetChecker()  
                {  
                      
                }  
                /** 
                 * 注册观察者对象 
                 * @param obj    观察者对象 
                 */  
                public static function attach(obj:INetObserver):void  
                {  
                    if(observerList.indexOf(obj) == -1)  
                    {  
                        observerList.push(obj);  
                    }  
                }  
                  
                /** 
                 * 删除观察者对象 
                 * @param obj    观察者对象 
                 */  
                public static function detach(obj:INetObserver):void{  
                    var index:int = observerList.indexOf(obj);  
                    if(index == -1)  
                        return;  
                    observerList.splice(index,1);  
                }  
                  
                /** 
                 * 通知所有注册的观察者对象 
                 */  
                private static function nodifyObservers():void{  
                    for each(var obj:INetObserver in observerList)  
                    {  
                        obj.updateNetAvailable(available);  
                    }  
                }  
                  
                /** 
                 * 开始监听网络状况  
                 *  
                 */  
                public static function monitorNetwork():void  
                {  
                    if(!monitor)  
                        monitor = new URLMonitor(request);  
                    monitor.pollInterval = _interval;  
                    monitor.addEventListener(StatusEvent.STATUS,onStatusHandler);  
                    monitor.start();  
                }  
                  
                /** 
                 * 设置监听网络间隔  
                 * @param val 
                 *  
                 */  
                public static function set interval(val:Number):void  
                {  
                    if(_interval == val)return;  
                    _interval = val;  
                    if(monitor && monitor.running)  
                    {  
                        monitor.stop();  
                        monitor.pollInterval = _interval;  
                        monitor.start();  
                    }  
                }  
                  
                public static function get interval():Number  
                {  
                    return _interval;  
                }  
                  
                /** 
                 * 停止监听网络状态  
                 *  
                 */  
                public static function stopMonitor():void  
                {  
                    if(monitor)  
                        monitor.stop();  
                }  
                  
                /** 
                 * 判断当前网络是否可用  
                 * @return  
                 *  
                 */  
                public static function get networkAvailable():Boolean  
                {  
                    return available;  
                }  
                  
                private static function onStatusHandler(e:StatusEvent):void  
                {  
        //          trace( ">>onStatusEvent::evt.code=" + e );  
                    if(e.level == "status")  
                    {  
                        var flag:Boolean = false;  
                        if(monitor.available && !available)  
                        {  
                            trace("网络已连接");  
                            available = true;  
                            flag = true;  
                        }  
                        else if(!monitor.available && available)  
                        {  
                            available = false;  
                            trace("没有检测到网络");  
                            flag = true;  
                        }  
                          
                        if(flag)  
                        {  
                            nodifyObservers();  
                        }  
                    }  
                          
                }  
                  
            }  
        }  
    
        package  
        {  
            public interface INetObserver  
            {  
                function updateNetAvailable(ava:Boolean):void;  
            }  
        }  
    
  • 相关阅读:
    CorelDRAW X8完全学习教程
    Linux环境编程:从应用到内核
    Excel 2010 VBA 入门与实战
    iOS 用GDataXMLNode创建和解析XML
    iOS 使用SBJSON创建和解析JSON
    [iOS]MD5加密字符串
    Read and Write NSArray, NSDictionary and NSSet to a File
    ASIHTTPRequest类库简介和使用说明
    ios 调用webservice整理
    数据库迁移(SQL SERVER导入数据到MySql)
  • 原文地址:https://www.cnblogs.com/skybdemq/p/4073030.html
Copyright © 2020-2023  润新知