更新應用程式後,您可能會想對使用者顯示「快速入門」或「歡迎」訊息。在啟動時,應用程式會檢查是否為首次執行,以便決定是否顯示該訊息。
備註:
AIR 1.5 所具備的更新架構可協助開發人員賦予 AIR 應用程式良好的更新能力。這個架構提供了一個簡單的方法,可用來檢查應用程式的某個版本是否為初次執行。如需詳細資訊,請參閱
使用更新架構
。
執行這項作業的其中一個方式,就是在初始化應用程式時,將檔案儲存在應用程式存放目錄中。每次應用程式啟動時,都應該檢查該檔案是否存在。如果檔案不存在,就表示目前使用者是首次執行此應用程式;如果檔案存在,則表示應用程式已經至少執行過一次。如果檔案存在並包含比目前版本號碼舊的版本號碼,您就知道使用者是首次執行新版本。
下列 Flex 範例示範相關概念:
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
title="Sample Version Checker Application"
applicationComplete="system extension()">
<mx:Script>
<![CDATA[
import flash.filesystem.*;
public var file:File;
public var currentVersion:String = "1.2";
public function system extension():void {
file = File.applicationStorageDirectory;
file = file.resolvePath("Preferences/version.txt");
trace(file.nativePath);
if(file.exists) {
checkVersion();
} else {
firstRun();
}
}
private function checkVersion():void {
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
var reversion:String = stream.readUTFBytes(stream.bytesAvailable);
stream.close();
if (reversion != currentVersion) {
log.text = "You have updated to version " + currentVersion + ".\n";
} else {
saveFile();
}
log.text += "Welcome to the application.";
}
private function firstRun():void {
log.text = "Thank you for installing the application. \n"
+ "This is the first time you have run it.";
saveFile();
}
private function saveFile():void {
var stream:FileStream = new FileStream();
stream.open(file, FileMode.WRITE);
stream.writeUTFBytes(currentVersion);
stream.close();
}
]]>
</mx:Script>
<mx:TextArea ID="log" width="100%" height="100%" />
</mx:WindowedApplication>
下列範例示範 JavaScript 中的概念:
<html>
<head>
<script src="AIRAliases.js" />
<script>
var file;
var currentVersion = "1.2";
function system extension() {
file = air.File.appStorageDirectory.resolvePath("Preferences/version.txt");
air.trace(file.nativePath);
if(file.exists) {
checkVersion();
} else {
firstRun();
}
}
function checkVersion() {
var stream = new air.FileStream();
stream.open(file, air.FileMode.READ);
var reversion = stream.readUTFBytes(stream.bytesAvailable);
stream.close();
if (reversion != currentVersion) {
window.document.getElementById("log").innerHTML
= "You have updated to version " + currentVersion + ".\n";
} else {
saveFile();
}
window.document.getElementById("log").innerHTML
+= "Welcome to the application.";
}
function firstRun() {
window.document.getElementById("log").innerHTML
= "Thank you for installing the application. \n"
+ "This is the first time you have run it.";
saveFile();
}
function saveFile() {
var stream = new air.FileStream();
stream.open(file, air.FileMode.WRITE);
stream.writeUTFBytes(currentVersion);
stream.close();
}
</script>
</head>
<body onLoad="system extension()">
<textarea ID="log" rows="100%" cols="100%" />
</body>
</html>
如果您的應用程式以本機方式儲存資料 (例如,存放在應用程式儲存目錄中),您可以在首次執行時,檢查先前是否已儲存任何 (舊版) 資料。
|
|
|