Last active
August 29, 2015 14:08
-
-
Save betesh/5a64628a6cd5152c291a to your computer and use it in GitHub Desktop.
Titanium code to automatically update an APK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var updateApk = function(apkUrl) { | |
var downloader = require("com.mykingdom.downloader").createAsyncDownloader({ | |
filesToDownload: [{url : apkUrl}], | |
outputDirectory: Ti.Filesystem.getFile("file:///mnt/sdcard/download"), | |
enableNotification: true, notificationId: 1, notificationTitle: "Downloading new APK" // Optional | |
}); | |
downloader.addEventListener("error", function(event){ | |
alert(event.error + " : While downloading file at (index 0 based) = " + event.currentIndex); | |
}); | |
downloader.addEventListener("onload", function(event){ | |
Ti.API.info("Downloading new APK (" + event.progress + "%)"); | |
}); | |
downloader.addEventListener("cancel", function(event){ | |
Ti.API.info("Hmmm, looks like the download was cancelled. Please try again."); | |
}); | |
downloader.addEventListener("success", function(event){ | |
var downloadedFile = 'file:///mnt/sdcard/download/' + event.source.outputDirectory.directoryListing[0]; | |
Ti.API.info("Downloaded new APK to " + downloadedFile); | |
var intent = Ti.Android.createIntent({action: Ti.Android.ACTION_VIEW, data: downloadedFile, type:'application/vnd.android.package-archive', packageName: 'com.android.packageinstaller' }); | |
intent.setFlags(Ti.Android.FLAG_ACTIVITY_NEW_TASK); | |
Ti.Android.currentActivity.startActivity(intent); | |
}); | |
downloader.startDownload(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment