Sencha Touch 2.3 + Cordova 3 環境搭建
採用Sencha Touch開發的App,如果使用者點選退出按鈕,則App會直接退出。
Sencha Cmd 4.0提供了訪問Cordova的方法,可以解決上述問題。
一、準備工作
1. 安裝Java JRE
2. 安裝Node.js
3. 安裝Cordova:在命令列中輸入npm install -g cordova
4. 下載ANT,並把ANT的路徑(例如:C:\apache-ant-1.9.4\bin)加入到系統環境變數path中
5. 下載Android SDK Manager,並把SDK的路徑(例如:C:\adt-bundle\sdk\tools)加入到系統環境變數path中
6. 安裝git,在Configuring the line ending conversions時選擇"Checkout as-is, commit as-is"這個選項
在Adjusting your PATH environment時選擇"Use Git from the Windows Command Prompt"
二、建立應用程式
1. 建立Sencha Touch專案:命令列切換到sencha touch sdk目錄,輸入sencha generate app SenToCo D:\sentoco
2. 啟用Cordova支援:命令列切換到專案目錄,輸入sencha cordova init com.mydomain.sentoco SenToCo
其中com.mydomain.sentoco是app id,SenToCo是工程的名稱,也是應用的名稱。
如果出現could not find file config.xml to copy的錯誤,找到檔案
Sencha\Cmd\4.0.2.67\extensions\cmd-cordova-packager\cmd-cordova-packager.plugin.xml
將檔案中的
<echo>Adding Cordova config.xml to App</echo>
<copy file="${app.cordova.www.dir}/config.xml" todir="${app.dir}"/>
改為
上述命令執行成功後目錄結構如下:<echo>Adding Cordova config.xml to App</echo> <copy file="${app.cordova.dir}/config.xml" todir="${app.dir}"/>
3. 修改專案目錄下cordova.local.properties檔案中的平臺列表
cordova.platforms=android
4. 在cordova中新增android平臺:命令列切換到專案目錄下的cordova目錄,輸入cordova platform add android
如果出現Please install Android Target 19(the Android newest
SDK). Make sure you have the latest Android tools installed as well的錯誤,則更新Android SDK
5.在cordova中新增Device功能(參考Apache Cordova Documentation):命令列切換到專案目錄下的cordova目錄,輸入
cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-device.git
如果新增成功則會出現以下介面:
下載後解壓(例如:D:\cordova_plugin\cordova-plugin-device-4ae85e8\),命令列切換到專案目錄下的cordova目錄,輸入
cordova plugin add D:\cordova_plugin\cordova-plugin-device-4ae85e8\cordova-plugin-device-4ae85e8
6. 在cordova中新增Notification功能(參考Apache Cordova Document):需要新增兩個功能外掛
https://git-wip-us.apache.org/repos/asf/cordova-plugin-vibration.git
https://git-wip-us.apache.org/repos/asf/cordova-plugin-dialogs.git
7. 在app.js中新增程式碼
launch: function() {
// Destroy the #appLoadingIndicator element
Ext.fly('appLoadingIndicator').destroy();
// Initialize the main view
Ext.Viewport.add(Ext.create('SenToCo.view.Main'));
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
// Register the event listener
document.addEventListener("backbutton", onBackKeyDown, false);
}
// Handle the back button
function onBackKeyDown() {
navigator.notification.confirm(
'確定退出應用?', // 顯示資訊
onConfirm, // 按下按鈕後觸發的回撥函式,返回按下按鈕的索引
'退出應用', // 標題
'取消,確定' // 按鈕標籤
);
}
function onConfirm(button) {
if (button == '2') {
navigator.app.exitApp();
}
}
},
8. 打包應用程式:命令列切換到專案目錄,輸入sencha app build native
如果全線飄綠就打包成功了,可以在D:\sentoco\cordova\platforms\android\ant-build目錄下找到apk檔案
參考: