NotificationType.CRITICAL只需要在加载文件是新文件时触发

时间:2013-01-15 21:19:34

标签: actionscript-3 air flash-cs5

我有一个内置在Flash cs5中的Adobe Air桌面留言板应用程序,它在动态文本字段中加载外部“.txt”文件,并每2分钟检查一个新文件。我需要它只在文件是新的时通知用户(NotificationType.CRITICAL),而不是每次加载它时。有可能吗?

应用中的所有代码:

NativeApplication.nativeApplication.startAtLogin=true

stage.nativeWindow.alwaysInFront=true;

//external text file load and recheck every 2 minutes

var myInterval:uint  = setInterval (loadUrl, 120000);
var loader:URLLoader = new URLLoader(new URLRequest("https://my_text_file.txt"));
loader.addEventListener(Event.COMPLETE, completeHandler);

function completeHandler(event:Event):void {
  var loadedText:URLLoader = URLLoader(event.target);
if(myText_txt.htmlText!=loadedText.data){
   myText_txt.htmlText = loadedText.data;
     stage.nativeWindow.notifyUser(NotificationType.CRITICAL) 
}else {
  //do nothing
   }
}

function loadUrl():void {
    loader = new URLLoader(new URLRequest("https:///my_text_file.txt"));
loader.addEventListener(Event.COMPLETE, completeHandler);
}

 // button control


Minimize_BTN.addEventListener(MouseEvent.CLICK, minimize);
function minimize(e:MouseEvent){
stage.nativeWindow.minimize();

}

drag_BTN.addEventListener(MouseEvent.MOUSE_DOWN, drag);
function drag(e:MouseEvent){
stage.nativeWindow.startMove();

}

stop(); //Stop on the frame you want

2 个答案:

答案 0 :(得分:0)

为什么不使用FileReference对象来查找某些属性,例如modificationDate,并根据此验证文件是否不同,您还可以测试它的大小。

答案 1 :(得分:0)

如果myText_txt字段正在修改加载的文本(例如,如果myText_txt的 condenseWhite 属性设置为true),则您的代码可能无效。确定文本是否已更改的更准确方法是将其存储在名为(例如 oldText )的String变量中,然后将 oldText 与新加载的文本进行比较。

以下是重写代码的一部分,以包含 oldText 变量。此代码也更有效,因为它只实例化一些变量,并避免重复某些代码:

import flash.net.URLRequest;

function completeHandler(event:Event):void
{
    var newText:String = loader.data;
    if(newText != oldText)
    {
            myText_txt.htmlText = newText;
            stage.nativeWindow.notifyUser(NotificationType.CRITICAL);
            oldText = newText;
    }
}

function loadUrl():void
{
    loader.load(req);
}

// Initialize your variables and event handlers only one time
var req:URLRequest = new URLRequest("https:///my_text_file.txt");

// Set cacheResponse to false to prevent a successful response
// from being cached.
req.cacheResponse = false;

// And don't bother checking the cache, either. 
// Not necessary, but the request will execute a little faster.
req.useCache = false;

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, completeHandler);

// Use oldText inside completeHandler() to determine 
// whether the file's text has changed
var oldText:String = "";

var myInterval:uint = setInterval(loadUrl, 120000);

// Start loading the text right away
loadUrl();