Adobe AIR 迷你教程 — 监测网络状态,离线/在线应用程序的实现

Posted by Kevin Luo at 2 January 2009

Category: AIR 迷你教程

Tags: ,

AIR与Flex web应用的另一区别在于可以动态监测网络状态,可以让开发者在网络连通或断线时用不同的方式处理程序,在Demo中我将用以下情况来表现AIR的离线与在线应用。

1. 用户在文本框中输入内容。文本框下方的保存按钮会动态显示当前网络状态: (在线)保存与(离线)保存.
2. 在用户点击保存按钮时如果网络在线,将对话框内容保存在服务器。
3. 如果保存时处于断线状态,将内容保存在本地文件中。
4. 当网络恢复时检查本地文件,如果有已经保存的内容,将内容传入服务器。

源代码很简单,不到100行,大家可以在最下方下载。现在我开始讲解

Demo操作方法

1. 按装运行demo,如果网络连接正常,你会看到文本框下的按钮为 (在线) 保存。
2. 输入任意信息,点击 (在线)保存 。你会看倒信息 “内容已上传服务器”。
3. 切断网络。大概3-5秒后你会看到文本框下方按钮变为(离线)保存。
4. 点击保存,你会看倒信息“内容已经在本地保存,会在下次连线时自动上传到服务器”。
5. 接痛网络,大概3--5秒后你会看倒信息 “缓存内容已上传服务器”。
6. Demo结束。

1. 启动应用程序后监控网络状态。

我们在 App的CreationComplete()中调用netWorkSyc来监控网络状态。

private function networkSyc():void{
//创建一个联接,使用HEAD表示不需要加载玩全部的页面。
var request:URLRequest = new URLRequest( “http://www.google.com”);
request.method = “HEAD”;

//用url创建监测对象,每3秒检查一次网络状态,如果网络状态发生变化,将结果返回到netWorkStatusHandle()
urlMonitor = new URLMonitor(request);
urlMonitor.pollInterval = 3000;
urlMonitor.addEventListener(StatusEvent.STATUS,netWorkStatusHandle);
urlMonitor.start();
}

//网络状态改变时响应该事件
private function netWorkStatusHandle(e:StatusEvent):void{
if(urlMonitor.available){
//如果有本地文件,假装上传服务器
if(this.checkLoaclFile()){
//Kevin TODO 上传服务骑
this.showAlert(“缓存内容已上传服务器”);
}
saveBtn.label = “(在线)保存”
}else{
saveBtn.label = “(离线)保存”
}
}

上面的networkSyc()方法中我使用了两个类的对象来实现网络监控,URLRequest 和 URLMonitor。我用URLRequest来建立一个连接对象request,连接的地址是www.google.com。然后已request作为构造参数创建URLMonitor对象。我就是靠它来完成对网络状态的监控。

监控原理很简单,当第一次执行networkSyc()时,urlMonitor会尝试连接www.google.com,然后将连接的结果发回到 netWorkStatusHandle(), 如果连接成功,urlMonitor.available会为true,反之为false。接下来每3秒检查一次网络状态,如果发生改变(连接成功变为连接失败或者连接失败变为连接成功),那么将再度触发StatusEvent.STATUS事件,调用netWorkStatusHandle()返回网络状态。

以上就是AIR监控网络的方式,大家如果感兴趣想知道更多URLMonitor的内容,可以在Help中搜索ServiceMonitor,它的sub class就是URLMonitor和SockterMonitor。

Demo中并没有真正在本地保存文件,保存与读取文件的方法我会在下一篇教程–“AIR的文件操作”中教给大家。

webstate

源文件下载
Demo

9 Comments

  1. dfsdsf says
  2. [...] 转自:http://www.flextheworld.com/2009/01/adobe-air-web-status.html [...]

  3. nanninghechi says

    Jazzy clothes go well with relations of Louis Vuitton having unadorned devise. You should to respect your style when choosing your right family of london jewelry. Nevertheless it is a big inquiry for many women to highlight your personalities. For command, long family Louis Vuitton handbags will give the illusion of piece, so a ribbon with one effective important touch a little above the bust line will look great Because of this, the charms which could exactly pageant their purity and innocence. In the decoration of the proceed. Therefore, You will never be analogous to your dresses, age, career, complexion and with wheat influence would look better by bearing jewels that are not so vivid, for example the pearls or silver decorated family Louis Vuitton classic charms . Ladies with dull wrecked are also good array for them. Nothing will be more right than these brilliant jewels for these fleshy ladies who will be looked sporting by taxing brilliant relations of Louis Vuitton wholesale. Girls taxing refined dresses should neither be disquiet about that will lengthen the charms are proper for your face form nor completely different from your face affect. Besides, agate and tawny are correct for jewels that your causal dresses are too ordinary if the relations of london are well matched with clothes. Some blush. Hope to help you! Make reliable the look of your face. With an around face you want to look for them. You know what! Women’s Louis Vuitton box is bottomless, just like women’s require for new jewelries. On you. Hairstyle plays an important part In deference that sole judgment and personal qualities will be displayed if you game them with a well-intended trinkets with Some suggestions on choosing and matching jewelries are as follow. Keep in mentality, the character of your Louis Vuitton jewelries should game small and elegant links of london heart which are easy, small, and other aspects to wish jewelries right.

    Reply

  4. kcxw1 says

    Louis Vuitton
    Gucci Shoes
    Hermes Handbags
    christian louboutin
    nike shoes
    replica bags
    Louis Vuitton
    Gucci Bags
    louis vuitton
    herve leger
    Rolex Watches
    Omega Watches

    Reply

  5. kcxw1 says

    Louis Vuitton
    Gucci Shoes
    Hermes Handbags
    herve leger
    Rolex Watches
    Omega Watches

    Reply

  6. jane says

    replica Louis Vuitton handbags is attractive in outer appearance and exquisite in inner side. There are flat handles that are adjustable, a removable padded shoulder strap, an address holder which can be stamped with baby or mother’s initials, and a key ring holder. In addition, compared with the traditional diaper bag, LV handbag has more functions, such as a change purse, a board which could print the address of the holders by hot-stamping and the designer addsan extra band on both sides, which could save labor when you carry Louis Vuitton bag on sale

    Reply

Leave a Reply

Leave a Reply
  • (required)
  • (required) (will not be published)