Here is some of the code I use for this purpose. You will have to adjust some as I am too lazy to strip it down atm. (sorry).
So, in onCreate of my Activity I have:
new FetchLatestVersion().execute();
And the code to check for and fetch latest version (+launch installer).
private class FetchLatestVersion extends AsyncTask<Void, Void, Integer> {
@Override
protected Integer doInBackground(Void... param) {
String inputLine = "-1";
BufferedReader in = null;
try {
URL update_url = new URL(UPDATE_VERSIONCHECK_URL);
in = new BufferedReader(new InputStreamReader(
update_url.openStream()));
while ((inputLine = in.readLine()) != null) {
return Integer.parseInt(inputLine);
}
} catch (IOException e) {
Log.e(TAG, inputLine, e);
} catch (NumberFormatException e) {
Log.e(TAG, inputLine, e);
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
}
}
return -1;
}
@Override
protected void onPostExecute(Integer result) {
if (result > device.getVersionCode()) {
// Toast.makeText(
// getApplicationContext(),
// "current version: " + device.getVersionCode() + "\n"
// + "new version: " + result, Toast.LENGTH_SHORT)
// .show();
// pref.putBoolean(PREF_HAS_UPDATE, true);
showDownloadOption();
}
}
}
private void showDownloadOption() {
findViewById(R.id.new_update).setVisibility(View.VISIBLE);
}
@OnClick(R.id.button_download)
public void update(Button button) {
if (mDownloadTask != null
&& mDownloadTask.getStatus() == Status.RUNNING) {
mDownloadTask.cancel(true);
}
mDownloadTask = new DownloadUpdate();
mDownloadTask.execute();
}
private class DownloadUpdate extends AsyncTask<Void, Void, File> {
public ProgressDialog working_dialog;
private String toastMsg = "";
@Override
protected void onPreExecute() {
working_dialog = ProgressDialog.show(context, "",
context.getString(R.string.working), true);
}
@Override
protected File doInBackground(Void... param) {
String filename = "YOURAPP.apk";
HttpURLConnection c = null;
try {
URL url = new URL(UPDATE_DOWNLOAD_URL);
c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
} catch (IOException e1) {
toastMsg = context
.getString(R.string.could_not_connect_to_server);
return null;
}
File myFilesDir = new File(Environment
.getExternalStorageDirectory().getAbsolutePath()
+ "/Download");
File file = new File(myFilesDir, filename);
if (file.exists()) {
file.delete();
}
if ((myFilesDir.mkdirs() || myFilesDir.isDirectory())) {
try {
InputStream is = c.getInputStream();
FileOutputStream fos = new FileOutputStream(myFilesDir
+ "/" + filename);
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
} catch (Exception e) {
toastMsg = context
.getString(R.string.missing_internet_connection);
}
}
return file;
}
@Override
protected void onPostExecute(File file) {
if (!toastMsg.isEmpty()) {
Toast.makeText(context, toastMsg, Toast.LENGTH_LONG).show();
} else {
launchInstaller(file);
}
removeWorkingDialog();
}
private void removeWorkingDialog() {
if (working_dialog != null && working_dialog.isShowing()) {
working_dialog.dismiss();
working_dialog = null;
}
}
}
public void launchInstaller(File apkFile) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(apkFile),
"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, 0);
}