使用air进行移动app开发常见功能和有关问题(二)

使用air进行移动app开发常见功能和问题(二)

1、  Air如何判断android、ios 平台网络连接状态?

Android,使用as3原生api:

    if(NetworkInfo.isSupported)//只有android支持
 {
                    NetworkInfo.networkInfo.addEventListener(Event.NETWORK_CHANGE,onNetWorkChanged);        
                    onNetWorkChanged();
           }
   private function onNetWorkChanged(e:Event = null):void
           {                          
                    var isActived:Boolean =false;
                   
                    varinterfaces:Vector.<NetworkInterface> =NetworkInfo.networkInfo.findInterfaces();
                   
                    varnetInterface:NetworkInterface;
                    for ( var i:int = 0, len:int= interfaces.length; i < len; i++)
                    {
                             netInterface =interfaces[i];
                             if(netInterface.name.toLowerCase()== "wifi" && netInterface.active) {
                                       isActived= true;
                                       break;
                             } elseif(netInterface.name.toLowerCase() == "mobile" &&netInterface.active) {
                                       isActived= true;
                                       break;
                             }
                    }
                    PhoneConfig.isOpenNet =isActived;
                   
                    if(isActived == false)
                    {
                             MsgManager.show("当前网络不可用,请检查你的网络设置。");
                    }
           }

           Ios平台,需要引入独立开发的ane扩展和类库,判断核心代码:

    if(netInterface.name.toLowerCase()== "en0" && netInterface.active) {//wifi
                    isActived= true;
                    break;
                }else if(netInterface.name.toLowerCase()== "pdp_ip0" && netInterface.active) {//gprs
                    isActived= true;
                    break;
             }
 

2、  iphone 5读不到正确尺寸问题

ios平台读取stageWidth和stageHeight方法和普通的air方法一致,但iphone5出现读取不正确的情况。

解决方法:需要根目录放一张名为Default-568h@2x.png尺寸为640*1136的图片,并且打包进去。

详情见http://zengrong.net/post/1752.htm#more-1752

 

3、  ios平台运行 release版本中途卡住问题

同一个文件 加载到当前域 加载第二次时 就会卡住(ipa调试版正常,release版就有问题;androidapk不论是调试版还是release都正常)

 

4、  打包ipa失败,提示无效的文件:

打包ipa时,如果文件里面有一个不正确格式的swf,打包必定失败;如果把swf换成其他不常用文件名,即可解决。

 

5、  android、ios平台最小化、关闭电源时app的检测和处理方式

手机测试结果:

1)android中 按home键或电源键都是使程序后台运行,socket不断。

2)ios中按home键是使程序后台运行,socket不断,按电源键程序后台运行,但socket断。

通过事件ACTIVATE、DEACTIVATE来进行代码逻辑控制

NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE,onActivate);

NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE,onDeactivate);

 

6、  android、ios上文件存储方法

核心代码:

	this.saveSDCardFile(File.applicationStorageDirectory.url+ "/aa.swf", btye);
(this.getSDCardFile(File.applicationStorageDirectory.url + "/aa.swf");
/**
         * 读取sd卡文件
         */
        private functiongetSDCardFile(url:String):ByteArray
        {
            var file:File =File.applicationStorageDirectory.resolvePath(url);
            if(file.exists == false)
                return null;
           
            var stream:FileStream = new FileStream();
            stream.open(file,FileMode.READ);
            var bytes:ByteArray = new ByteArray;
            stream.readBytes(bytes,0, stream.bytesAvailable);
            stream.close();
            return bytes;
        }
        /**
         * 往sd卡上存储文件
         */
        private functionsaveSDCardFile(url:String, bytes:ByteArray):void
        {
            var file:File =File.applicationStorageDirectory.resolvePath(url);
            var stream:FileStream = new FileStream();
            stream.open(file,FileMode.WRITE);
            stream.writeBytes(bytes);
           
            stream.close();
     }

注意android和ios上一些目录路径不一样

    trace( File.applicationDirectory.url);//App:/ (/data/data/app.appId/app/assets)
                    trace(File.applicationStorageDirectory.url);//|app-storage:/ (/data/data/app.appID/appID/LocalStore)
                    trace(File.documentsDirectory.url);
                    trace(File.userDirectory.url);
                    trace(File.desktopDirectory.url);
                    trace(File.createTempDirectory().url);//data/data/app.appId/cache
                    //android:
//                  app:/
//                  app-storage:/
//                  file:///storage/sdcard0
//                  file:///storage/sdcard0
//                  file:///storage/sdcard0
//                  file:///data/data/air.TestLoad.debug/cache/FlashTmp.u15893
                    //ios:
//                  app:/
//                  app-storage:/
//                  file:///var/mobile/Applications/EBFEE682-C347-4BC1-9264-6E4B65F4D2BA/Documents
//                  file:///var/mobile/Applications/EBFEE682-C347-4BC1-9264-6E4B65F4D2BA
//                  file:///var/mobile/Applications/EBFEE682-C347-4BC1-9264-6E4B65F4D2BA/Desktop
//                  file:///private/var/mobile/Applications/EBFEE682-C347-4BC1-9264-6E4B65F4D2BA/tmp/FlashTmp.bDIE2x

Android 和 ios 文件系统差异http://www.cnblogs.com/sevenyuan/archive/2013/03/07/2948300.html

 

7、  游戏过程中的待机处理,比如在激烈pk中不允许手机进入睡眠状态:

通过设置NativeApplication.nativeApplication.systemIdleMode属性

/**
         * 是否关闭 休眠模式
         * @param value
         */
        public functionkeepAwake(value:Boolean):void
        {
            if(value)
                NativeApplication.nativeApplication.systemIdleMode= SystemIdleMode.KEEP_AWAKE;
            else
                NativeApplication.nativeApplication.systemIdleMode= SystemIdleMode.NORMAL ;
        }

8、  如何读取air的app配置文件?如何读取里面的版本号?

//版本号
public function get version():String
        {
            var appDescriptor:XML =NativeApplication.nativeApplication.applicationDescriptor;//配置文件
            var ns:Namespace =appDescriptor.namespace();
            return "" +appDescriptor.ns::versionNumber;
        }


9、  统计流量功能(未找到as3原生api支持,可通过第三方ane扩展实现)


10、手机加载大文件解压缩很慢,卡住问题(暂无较好的规避方法)


11、  检测鼠标按下菜单键、及其他键的方法及响应

NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN,this.keyHandler);
private function keyHandler(e:KeyboardEvent):void
                   {
                            if(e.keyCode==Keyboard.BACK)
                            {
                                     e.preventDefault();  //取消返回键的退出事件
                                    
                                     ExitPane.instance.show();
                            }
                            elseif(e.keyCode== Keyboard.MENU)
                            {
                                     this.times++;
                                     if(times>= 5)
                                               ClientConfig.webDebug= true;
                            }
                            elseif(e.keyCode == Keyboard.SEARCH)
                            {
                                    
                            }
                            else  if (e.keyCode == Keyboard.HOME) {
                                     //Handle Home button.
                            }
                   }

12、  手机调试不方便怎么办?设置手机代理,把ip指向电脑,这样所有数据包就会通过电脑发送和接受,用电脑抓包调试。

 

13、  其他知识点:

运行ios发行版ipa特有问题:

1)uint和int的比较:int类型的-1大于uint类型的0;

2)String(null) 依旧是null值,而不是字符串的"null"

 

NativeApplication 的exit() 方法不会导致调度exiting 事件;

android中任务管理器中结束应用也不会派发exit事件,ios中未知

ios下通过任务管理器关闭app,app本身无法控制退出

  

flash builder 编译提示错误:Erroroccurred while packaging the application: Map failed

解决办法:一般清理一下项目就ok了

 

stage.displayState =StageDisplayState.FULL_SCREEN

设置全屏后立刻读取stagewidth和stageheight是不准确的,需要延时到下一帧再读取

 

桌面 移动设备 api支持的差异 http://help.adobe.com/en_US/air/build/WS144092a96ffef7cc16ddeea2126bb46b82f-8000.html

adt 打包时目标参数详解 http://help.adobe.com/zh_CN/air/build/WS901d38e593cd1bac1e63e3d128cdca935b-8000.html