Ionic Auto Upgrade App (Android version)

Source: Internet
Author: User


Ionic Framework Introduction

Ionic is a angularjs-based, user-interface framework that uses HTML5 to build hybrid mobile apps, claiming to be "a combination of local and HTML5." The framework provides a number of basic mobile user interface examples, such as simple entries like List (lists), tab bar (tab bars), and trigger switch (toggle switches). It also provides examples of more complex visual layouts, such as the slide-out menu showing content below.
Ionic Auto-upgrade app

First, the preparatory work

1.Cordova Plugins:

Cordova Plugin Add https://github.com/whiteoctober/cordova-plugin-app-version.git//Get App version
Cordova Plugin Add org.apache.cordova.file//File system
Cordova Plugin Add org.apache.cordova.file-transfer//File transfer system
Cordova Plugin Add https://github.com/pwlin/cordova-plugin-file-opener2//File Open System

2.AngularJS Cordova Plug-in

Ngcordova

Second, the relevant code, App.js
Copy Code

. Run ([' $ionicPlatform ', ' $rootScope ', ' $ionicActionSheet ', ' $timeout ', ' $cordovaAppVersion ', ' $ionicPopup ', ' $ Ionicloading ', ' $cordovaFileTransfer ', ' $cordovaFile ', ' $cordovaFileOpener 2 ', 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 ();
}

Detect Updates
Checkupdate ();

Document.addeventlistener ("Menubutton", Onhardwaremenukeydown, false);
});


Menu key
function Onhardwaremenukeydown () {
$ionicActionSheet. Show ({
TitleText: ' Check for updates ',
Buttons: [
{text: ' About '}
],
Destructivetext: ' Check for updates ',
Canceltext: ' Cancel ',
Cancel:function () {
Add Cancel Code:
},
Destructivebuttonclicked:function () {
Check for Updates
Checkupdate ();
},
Buttonclicked:function (index) {

}
});
$timeout (function () {
Hidesheet ();
}, 2000);
};

Check for Updates
function Checkupdate () {
var serverappversion = "1.0.0"; Get the latest version from the server
Get version
$cordovaAppVersion. Getappversion (). Then (version) {
If the app version on the local and server side does not match
if (version! = serverappversion) {
Showupdateconfirm ();
}
});
}

Show whether to update the dialog box
function Showupdateconfirm () {
var confirmpopup = $ionicPopup. Confirm ({
Title: ' Version upgrade ',
Template: ' 1.xxxx;</br>2.xxxxxx;</br>3.xxxxxx;</br>4.xxxxxx ',//get updated content from the server
Canceltext: ' Cancel ',
Oktext: ' Upgrade '
});
Confirmpopup.then (function (res) {
if (res) {
$ionicLoading. Show ({
Template: "Downloaded: 0%"
});
var url = "http://192.168.1.50/1.apk"; The path to the update app can be obtained from the server
var TargetPath = "file:///storage/sdcard0/Download/1.apk"; The path to the app download, which can be configured using the Cordova file plugin
var trusthosts = True
var options = {};
$cordovaFileTransfer. Download (URL, TargetPath, Options, trusthosts). Then (function (Result) {
Open the Downloaded app
$cordovaFileOpener 2.open (TargetPath, ' application/vnd.android.package-archive '
). Then (function () {
Success
}, function (err) {
Error
});
$ionicLoading. Hide ();
}, function (err) {
Alert (' download failed ');
}, function (progress) {
Progress, use text here to show download percentage
$timeout (function () {
var downloadprogress = (progress.loaded/progress.total) * 100;
$ionicLoading. Show ({
Template: "Already downloaded:" + math.floor (downloadprogress) + "%"
});
if (DownloadProgress > 99) {
$ionicLoading. Hide ();
}
})
});
} else {
Cancel Update
}
});
}
}])

The above is a simple implementation, some of the data are written here dead, you can get some data from the server, such as the latest version number, the most recent download path, here is a thought.

Project Address: HTTPS://GITHUB.COM/ZXJ963577494/IONIC-AUTOUPDATEAPP

Simply execute Ionic build Android



-this article reprinted-reprinted from: http://www.cnblogs.com/zxj159/p/4421578.html

Ionic Auto Upgrade App (Android version)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.