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

. Run ([' $ionicPlatform ', ' $rootScope ', ' $ionicActionSheet ', ' $timeout ', ' $cordovaAppVersion ', ' $ionicPopup ', ' $ Ionicloading ', ' $cordovaFileTransfer ', ' $cordovaFile ', ' $cordovaFileOpener 2 ',function($ionicPlatform, $rootScope, $ionicActionSheet, $timeout, $cordovaAppVersion, $ionicPopup, $ionicLoading, $ Cordovafiletransfer, $cordovaFile, $cordovaFileOpener 2) {$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 RequiredStatusbar.styledefault (); }            //Detect Updatescheckupdate (); Document.addeventlistener ("Menubutton", Onhardwaremenukeydown,false);        }); //Menu Key        functionOnhardwaremenukeydown () {$ionicActionSheet. Show ({titletext:' Check for updates ', buttons: [{text:On}], Destructivetext:' Check for updates ', Canceltext:Cancel, Cancel:function () {                    //Add cancel code:}, destructivebuttonclicked:function () {                    //Check for Updatescheckupdate (); }, ButtonClicked:function(Index) {}}); $timeout (function() {hidesheet (); }, 2000);        }; //Check for Updates        functioncheckupdate () {varServerappversion = "1.0.0";//get the latest version from the server            //Get version$cordovaAppVersion. Getappversion (). Then (function(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        functionshowupdateconfirm () {varConfirmpopup =$ionicPopup. Confirm ({title:' Version upgrade ', Template:' 1.xxxx;</br>2.xxxxxx;</br>3.xxxxxx;</br>4.xxxxxx ',//get updated content from the serverCanceltext: ' Cancel ', Oktext:Upgrade            }); Confirmpopup.then (function(res) {if(res) {$ionicLoading. Show ({Template:"Downloaded: 0%"                    }); varurl = "http://192.168.1.50/1.apk";//the path to the update app can be obtained from the server                    varTargetPath = "file:///storage/sdcard0/Download/1.apk";//the path to the app download, which can be configured using the Cordova file plugin                    varTrusthosts =true                    varOptions = {}; $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 () {                            varDownloadProgress = (progress.loaded/progress.total) * 100; $ionicLoading. Show ({Template:"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

    

Ionic Auto Upgrade App (Android version)

Related Article

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.