VersionChecker.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import android.os.AsyncTask; import org.jsoup.Jsoup; import java.io.IOException; public class VersionChecker extends AsyncTask<String, String, String> { String newVersion; @Override protected String doInBackground(String... params) { try { newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + "[your package]" + "&hl=en") .timeout(30000) .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6") .referrer("http://www.google.com") .get() .select("div[itemprop=softwareVersion]") .first() .ownText(); } catch (IOException e) { e.printStackTrace(); } return newVersion; } } |
input below code in MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); VersionChecker versionChecker = new VersionChecker(); try { String latestVersion = versionChecker.execute().get(); String versionName = BuildConfig.VERSION_NAME; if (latestVersion != null && !latestVersion.isEmpty()) { if (!latestVersion.equals(versionName)) { dialogBox(); } } Log.d("update", "Current version " + Float.valueOf(versionName) + ", Playstore version " + Float.valueOf(latestVersion)); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } public void dialogBox() { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom)); alertDialogBuilder.setTitle("Update required"); alertDialogBuilder.setMessage("An importan new version of Lias Trans is available. You need to update before you can continue using Lias Trans"); alertDialogBuilder.setCancelable(false); alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { Intent intent = new Intent(Intent.ACTION_VIEW ,Uri.parse("market://details?id=[your package]")); startActivity(intent); } }); alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { finish(); } }); AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show(); } |