Skip to content

Instantly share code, notes, and snippets.

@betesh
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save betesh/5a64628a6cd5152c291a to your computer and use it in GitHub Desktop.
Save betesh/5a64628a6cd5152c291a to your computer and use it in GitHub Desktop.
Titanium code to automatically update an APK
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