我們知道在Ubuntu Touch上面,我們可以建立Qt/QML的應用,同時,我們也可以使用Ubuntu SDK來建立一些跨平台的HTML 5的應用。這些應該雖然是在Ubuntu Touch的SDK上面建立的,但也是可以修改在其它的平台上運行。下面,我們來簡單介紹一些怎麼在SDK中建立並部署.
在Ubuntu Touch上面,我們可以使用HTML 5的一些tag來些,我們的應用,同時我們也可以使用Cordova API介面來擴充我們的功能。更多的閱讀,可以閱讀我們的官方網站
HTML5 Guide Cordova Guide 你可以在Ubuntu的開發人員網站找到更多的關於這些API的介紹:http://developer.ubuntu.com/api/html5/development/
1) 建立一個基本的架構
首先我們開啟我們的SDK,可以看到如下的介面:
接下來我們可以建立一個稱作“WebSysInfo"的應用。這個應用的目的是為了顯示一些系統的一些資訊。
緊接著,按照SDK的提示完成剩下的步驟來建立一個最基本的架構應用。由於HTML 5的應用是跨平台的,我們可以直接在電腦上運行這個最基本的應用,也可以直接部署到手機上。如果你在這個步驟不能正確地運行你的應用,請查看我的 SDK安裝指南。 如果你運行程式正常,你可以看到如下的畫面:
至此,我們已經完成了一個最基本的應用。下面我們來學習如何添加Cordova API 到我們的應用中來擴充我們的一些功能。
2)添加Cordova API到應用中 我們知道Cordova API有很多有用的功能。可以是我們的應用更加豐富。下面我們來學習怎麼把 Cordova API加到我們的應用中去。首先,我們選中我們的項目,同時選擇菜單“ Tool”==>"Ubuntu" ==>"Add Cordova runtime to HTML 5 project"。
這時應用開始下載最新的Cordova API的包,並安裝到我們所在的應用中。目前由於一些原因,需要關閉項目(最終的版本應該解決這個問題),再重新開啟項目來查看在項目欄更新後的目錄結構。
為了使用這些API,我們可以對"index.html"的代碼做如下的修改。
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>An Ubuntu HTML5 application</title> <meta name="description" content="An Ubuntu HTML5 application"> <!-- due to a bug in the SDK, the following line will be used in the final SDK <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"> --> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Ubuntu UI Style imports - Ambiance theme --> <link href="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/css/appTemplate.css" rel="stylesheet" type="text/css" /> <!-- Ubuntu UI javascript imports - Ambiance theme --> <script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/fast-buttons.js"></script> <script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/core.js"></script> <script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/buttons.js"></script> <script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/dialogs.js"></script> <script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/page.js"></script> <script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/pagestacks.js"></script> <script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/tab.js"></script> <script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/tabs.js"></script> <!-- Cordova platform API access - Uncomment this to have access to the Cordova Javascript APIs --> <!-- NOTE: Make sure that you have imported the Cordova native runtime and JS libraries into your project first. In order to do that, in QtCreator, select the "Add Cordova runtime to HTML5 project" menu item in Tools > Ubuntu. --> <script src="cordova/cordova.js"></script> <!-- Application script --> <script src="js/app.js"></script> </head> <body> <div data-role="mainview"> <header data-role="header"> <ul data-role="tabs"> <li data-role="tabitem" data-page="mainpage">System Info</li> </ul> </header> <div data-role="content"> <!-- The application main page --> <div data-role="tab" id="mainpage"> <section data-role="list" id="info"> <header class="large-font">System Info</header> </section> </div> </div> </div> </body></html>
這裡我們定義了一個“System Info”的tab。它對應著"mainpage"。當它被點擊的時候,對應的"mainpage"就會被開啟。在“mainpage"中我們定義了一個section, 在section中,定義了一個list。我們為list也定義了一個叫做"System Info”的"header"。當然我麼也可以定義更多的"header"。值得注意的是我們通過一下語句
<script src="cordova/cordova.js"></script>
來載入Cordova API以使我們能夠正常使用們。同時,
<!-- Application script -->
<script src="js/app.js"></script>
我們可以使用"app.js"來對我們的HTML進行操作。
3)怎麼調試應用 當我們運行應用的時候,我們可以在"Application output"視窗看到如下的資訊:
unity::action::ActionManager::ActionManager(QObject*):Could not determine application identifier. HUD will not work properly.Provide your application identifier in $APP_ID environment variable.Using "/home/liuxg/cases/WebSysInfo/www" as working dir "file:///home/liuxg/cases/WebSysInfo/www/index.html" Using "/home/liuxg/cases/WebSysInfo/www" as working dir "file:///home/liuxg/cases/WebSysInfo/www/index.html" Inspector server started successfully. Try pointing a WebKit browser to http://192.168.199.228:9221
顯然它告訴我們我們可以在Chrome瀏覽器中開啟
http://192.168.199.228:9221地址查看我們所需要的資訊。我們不妨在“app.js"檔案裡輸入如下的句子
window.onload = function () { var UI = new UbuntuUI(); UI.init(); console.log("we are so glad to see it works!") // Add an event listener that is pending on the initialization // of the platform layer API, if it is being used. document.addEventListener("deviceready", function() { if (console && console.log) console.log('Platform layer API ready'); }, false);};
運行程式,同時在Chrome中開啟
http://192.168.199.228:9221。可以看到如下的畫面:
我們可以在“Console”裡看到我們需要的輸出。必須注意的是,如果一個應用有兩個同時啟動並執行執行個體,那麼最新的執行個體將不會有任何的輸出。在Console裡只能看到第一個執行個體的輸出。當你運行第二個執行個體的時候,你可以在Application Output 視窗中看到如下的輸出:
Couldn't start the inspector server on bind address "192.168.199.228" and port "9221". In case of invalid input, try something like: "12345" or "192.168.2.14:12345"
另外應用必須先啟動,然後再看地址
http://192.168.199.228:9221。否則你將進不去。
4) 加入JS代碼實現Cordova API的調用 現在我們來修改項目中“app.js”檔案來對UI進行改變。首先我們在"index.html"中加入如下的句子:
<script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/list.js"></script>
這樣,我們就可以在"app.js"中使用如下的句子來得到我們的list了:
var info = UI.list('#info');
有了變數info,我們就可以向其中添加我們所需要的東西了。
處理window.onload事件 對於Web開發人員來講,window.onload事件並不陌生。當DOM被完全載入時,這個事件會被發生。這個事件對於處理一些需要在DOM完全載入緊接著執行一些代碼。
window.onload = function () {....}
處理deviceready事件 對於使用Cordova API的應用來說,我們需要使用deviceready事件來完成對Cordova API的使用。這個事件是用來告訴Cordova runtime已經準備好了可以使用了。一般來說,它的使用是這樣的
/** * Wait before the DOM has been loaded before initializing the Ubuntu UI layer */window.onload = function () { var UI = new UbuntuUI(); UI.init(); console.log("we are so glad to see it works!") var info = UI.list('#info'); // Add an event listener that is pending on the initialization // of the platform layer API, if it is being used. document.addEventListener("deviceready", function() { if (console && console.log) console.log('Platform layer API ready'); info.append("Cordova version: " + device.cordova, null, null, null); info.append("Device model: " + device.model, null, null, null); info.append("Version: " + device.version, null, null, null); info.append("Platform: " + device.platform, null, null, null); info.append("UUID: " + device.uuid, null, null, null); info.append("Available: " + device.available, null, null, null); info.append("Screen width: " + screen.width); info.append("Screen height: " + screen.height); info.append("Colordepth: " + screen.colorDepth); info.append("UserAgent: " + navigator.userAgent); }, false);};
這樣我們的一個簡單的應用就已經做好了。在Nexus 4上面我們可以看到如下的畫面
至此,我們一個最基本的HTML 5的應用已經做好了。源碼可以在如下的地址找到。
bzr branch lp:~liu-xiao-guo/debiantrial/websysinfo