Skip to content

Instantly share code, notes, and snippets.

@Pamblam
Last active April 2, 2018 17:55
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 Pamblam/53cb2b1c90dea09d4872084956d17b5c to your computer and use it in GitHub Desktop.
Save Pamblam/53cb2b1c90dea09d4872084956d17b5c to your computer and use it in GitHub Desktop.
Check for updates in the play store and prompt user to update if an update is available.
package com.geneticcoder.Versionator;
// Check here for updates: https://gist.github.com/Pamblam/53cb2b1c90dea09d4872084956d17b5c
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.app.AlertDialog;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
public class Versionator {
private Context context;
public static String packageName = null;
public Versionator(Context c){
context = c;
packageName = context.getPackageName();
}
public Versionator setPackageName(String packageName){
this.packageName = packageName;
return this;
}
public void checkUpdate(){
new AsyncTask<String, Void, Bundle>() {
@Override
protected Bundle doInBackground(String... params) {
String html = "<div class=\"content\" itemprop=\"softwareVersion\"> 0 </div>";
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://play.google.com/store/apps/details?id="+packageName+"&hl=en")
.build();
Response response = client.newCall(request).execute();
html = response.body().string();
}catch (IOException e){e.printStackTrace();}
html = html.substring(html.indexOf("Current Version</div><div><span class=\"")+46);
html = html.substring(0,html.indexOf("<"));
float vn = Float.valueOf(html);
float cvn = 0;
try {
String vname = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
cvn = Float.valueOf(vname);
}catch (PackageManager.NameNotFoundException e){e.printStackTrace();}
Bundle b = new Bundle();
b.putFloat("LatestVersion",vn);
b.putFloat("CurrentVersion",cvn);
b.putBoolean("UpdateAvailable", vn>cvn);
return b;
}
@Override
protected void onPostExecute(Bundle result) {
if(result.getBoolean("UpdateAvailable")){
updateAvailable(result);
}else{
done();
}
}
}.execute();
}
// override this...
public void done(){
Log.i("Versionator", "No updates available");
}
public void updateAvailable(Bundle updateInfo){
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setCancelable(false);
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.setTitle("Update Available!");
alertDialog.setMessage("Version "+updateInfo.getFloat("LatestVersion")+" has been released. "+
"You are currently using version "+updateInfo.getFloat("CurrentVersion")+". "+
"Would you like to update now?");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Update",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
try {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName)));
} catch (android.content.ActivityNotFoundException anfe) {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + packageName)));
}
dialog.dismiss();
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Not now",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
done();
dialog.dismiss();
}
});
alertDialog.show();
Log.i("Versionator", "Updates available");
}
}

Updated Apr. 4 '18 to take into account recent changes in Playstore

Notes & prerequisites

  • Uses the version name, not the build number. The build number is not publicly available on the play store. Therefore, version name must be castable to a float.
  • Requires okhttp & internet permissions
  • Don't forget to change the package name in the class file
  • If update is available, will show dialog asking user to update. If user chooses to update, will open the play store. If not, the done() method is called.

Requires okhttp & internet permissions...

In the gradle file...

dependencies {
    compile 'com.squareup.okhttp:okhttp:2.5.0'
}

In the manifest...

<uses-permission android:name="android.permission.INTERNET"/>

Sample usage

new Versionator(this){
    @Override
    public void done(){
        // Check is done, no update is available
        // Navigate to next activity or whatever..
    }
}.checkUpdate();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment