一、準備工作
1.Cordova插件:
cordova plugin add https://github.com/whiteoctober/cordova-plugin-app-version.git // 擷取APP版本
cordova plugin add org.apache.cordova.file // 檔案系統
cordova plugin add org.apache.cordova.file-transfer //檔案傳輸系統
cordova plugin add https://github.com/pwlin/cordova-plugin-file-opener2 //檔案打開系統
2.AngularJS Cordova插件
ngCordova
二、相關代碼,app.js
.run(['$ionicPlatform', '$rootScope','$ionicActionSheet', '$timeout','$cordovaAppVersion', '$ionicPopup', '$ionicLoading','$cordovaFileTransfer', '$cordovaFile', '$cordovaFileOpener2', function ($ionicPlatform, $rootScope,$ionicActionSheet, $timeout, $cordovaAppVersion, $ionicPopup, $ionicLoading, $cordovaFileTransfer, $cordovaFile, $cordovaFileOpener2) {
$ionicPlatform.ready(function ($rootScope) {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
//檢測更新
checkUpdate();
document.addEventListener("menubutton", onHardwareMenuKeyDown, false);
});
// 菜單鍵
function onHardwareMenuKeyDown() {
$ionicActionSheet.show({
titleText: '檢查更新',
buttons: [
{ text: '關于' }
],
destructiveText: '檢查更新',
cancelText: '取消',
cancel: function () {
// add cancel code..
},
destructiveButtonClicked: function () {
//檢查更新
checkUpdate();
},
buttonClicked: function (index) {
}
});
$timeout(function () {
hideSheet();
}, 2000);
};
// 檢查更新
function checkUpdate() {
var serverAppVersion = "1.0.0"; //從服務端擷取最新版本
//擷取版本
$cordovaAppVersion.getAppVersion().then(function (version) {
//如果本地于服務端的APP版本不符合
if (version != serverAppVersion) {
showUpdateConfirm();
}
});
}
// 顯示是否更新對話框
function showUpdateConfirm() {
var confirmPopup = $ionicPopup.confirm({
title: '版本更新',
template: '1.xxxx;</br>2.xxxxxx;</br>3.xxxxxx;</br>4.xxxxxx', //從服務端擷取更新的内容
cancelText: '取消',
okText: '更新'
});
confirmPopup.then(function (res) {
if (res) {
$ionicLoading.show({
template: "已經下載下傳:0%"
});
var url = "http://192.168.1.50/1.apk"; //可以從服務端擷取更新APP的路徑
var targetPath = "file:///storage/sdcard0/Download/1.apk"; //APP下載下傳存放的路徑,可以使用cordova file插件進行相關配置
var trustHosts = true
var options = {};
$cordovaFileTransfer.download(url, targetPath, options, trustHosts).then(function (result) {
// 打開下載下傳下來的APP
$cordovaFileOpener2.open(targetPath, 'application/vnd.android.package-archive'
).then(function () {
// 成功
}, function (err) {
// 錯誤
});
$ionicLoading.hide();
}, function (err) {
alert('下載下傳失敗');
}, function (progress) {
//進度,這裡使用文字顯示下載下傳百分比
$timeout(function () {
var downloadProgress = (progress.loaded / progress.total) * 100;
$ionicLoading.show({
template: "已經下載下傳:" + Math.floor(downloadProgress) + "%"
});
if (downloadProgress > 99) {
$ionicLoading.hide();
}
})
});
} else {
// 取消更新
}
});
}
}])
上面是一個簡單實作方式,一些資料都在這裡寫死了,你可以将一些資料從服務端擷取,比如最新版本号,最新版的下載下傳路徑,這裡提供一個思路。
隻需執行ionic build android即可
原文轉發自http://ionichina.com/topic/552b511e154e532908b07800,用以資料留存