Hi Ive got an app that im trying to add a built in updater to. The app contains a button that downloads an apk file from my server. The file names on the server are named as follows:
"App-1.apk", "App-2.apk", "App-3.apk" ...
The button is currently setup like this:
download = (Button) findViewById(R.id.bDownload);
versionNum = 3;
download.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent downloadFromServer = new Intent();
downloadFromServer.setAction(Intent.ACTION_VIEW);
downloadFromServer.addCategory(Intent.CATEGORY_BROWSABLE);
downloadFromServer.setData(Uri.parse("http://server.com/Files/App-" + versionNum + ".apk"));
startActivity(downloadFromServer);
}
});
What would be a good way to check for the highest available app version on the server, and pick that one to download?
Edit: How can I use java to check the server directory for the highest numbered app?
Edit2: Heres what i ended up doing. Not the best solution, but good enough for now:
try {
PackageInfo appInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
installedVersion = appInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
// Handle exception
}
//Latest version available on my server. Must update this value for each new release
latestVersion = 3.1;
//Convert string value of installed version to double so that it can be compared with value of latest version
installedVersionValue = Double.parseDouble(installedVersion);
download = (Button) findViewById(R.id.bDownload);
download.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (installedVersionValue<latestVersion) { //If latest version available on server is greater than installed version, download the latest
Intent downloadFromServer = new Intent();
downloadFromServer.setAction(Intent.ACTION_VIEW);
downloadFromServer.addCategory(Intent.CATEGORY_BROWSABLE);
downloadFromServer.setData(Uri.parse("http://server.com/Files//App-" + latestVersion + ".apk"));
startActivity(downloadFromServer);
}
else if (installedVersionValue==latestVersion) { //If user clicks the update button while they already have the latest, let them no what's up
Toast.makeText(getApplicationContext(), "You are already running the latest version (" + installedVersionValue +")",
Toast.LENGTH_LONG).show();
}
}
});