Checking Android version in app

I want to check the version of the application on Google Play when my application opens. If the application has a higher version than the installed application, I want to notify the user about the application update. I found that the “android request” is from here , in this I cannot dynamically check the version, I suppose, to install Major, Minor or Revision. Someone please help me how can I do this?

Thank you in advance

+8
android
Aug 05 '13 at 4:58
source share
2 answers

Basically, you should check the latest version of your application on the market and the version of the application on the device and decide if an update is available. To do this, try the following:

You should use this to get the current version (the version of the application on the device):

private String getCurrentVersion(){ PackageManager pm = this.getPackageManager(); PackageInfo pInfo = null; try { pInfo = pm.getPackageInfo(this.getPackageName(),0); } catch (PackageManager.NameNotFoundException e1) { e1.printStackTrace(); } String currentVersion = pInfo.versionName; return currentVersion; } 

And use this to get the latest version in a google game (taken from https://stackoverflow.com/a/127263/... ):

 private class GetLatestVersion extends AsyncTask<String, String, String> { String latestVersion; @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected String doInBackground(String... params) { try { //It retrieves the latest version by scraping the content of current version from play store at runtime String urlOfAppFromPlayStore = "https://play.google.com/store/apps/details?id= your app package address"; Document doc = Jsoup.connect(urlOfAppFromPlayStore).get(); latestVersion = doc.getElementsByAttributeValue("itemprop","softwareVersion").first().text(); }catch (Exception e){ e.printStackTrace(); } return latestVersion; } } 

Then, when your application starts, check them against each other as follows:

 String latestVersion = ""; String currentVersion = getCurrentVersion(); Log.d(LOG_TAG, "Current version = " + currentVersion); try { latestVersion = new GetLatestVersion().execute().get(); Log.d(LOG_TAG, "Latest version = " + latestVersion); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } //If the versions are not the same if(!currentVersion.equals(latestVersion)){ final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("An Update is Available"); builder.setPositiveButton("Update", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Click button action startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=your app package address"))); dialog.dismiss(); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Cancel button action } }); builder.setCancelable(false); builder.show(); } 

And show the user update dialog box. But make sure you have already imported the jsoup library.

Optional: to import the jsoup library, follow these steps:

1- go to the "File" menu
2- design structure
3 - left side click on the application
4- Select the dependencies tab
5- Click + 6 - Click the library dependency link
7- Search for "jsoup"
8 - select org.jsoup: jsoup and click ok

+9
Nov 25 '15 at 19:36
source share

If you do not want to use the Jsoup library, you can use something like this to get the current version number on Google Play:

 import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.net.MalformedURLException; import java.net.URL; public class StackAppVersion { public static void main(String[] args) { try { System.out.println(currentVersion()); } catch (IOException ex) { System.out.println("Failed to read Google Play page!"); } } private static String currentVersion() throws IOException { StringBuilder sb = new StringBuilder(); try (Reader reader = new InputStreamReader( new URL("https://play.google.com/store/apps/details?id=com.stackexchange.marvin&hl=en") .openConnection() .getInputStream() , "UTF-8" )) { while (true) { int ch = reader.read(); if (ch < 0) { break; } sb.append((char) ch); } } catch (MalformedURLException ex) { // Can swallow this exception if your static URL tests OK. } String parts[] = sb.toString().split("softwareVersion"); return parts[1].substring( parts[1].indexOf('>') + 1, parts[1].indexOf('<') ).trim(); } } 

If you store "& hl = en" in the URL, the UTF-8 character encoding should be fine.

+5
May 15 '16 at 1:32
source share



All Articles