I want to check application version on google play when my app open. If App has higher version than the installed app, I want to notify user to update the app. I found that
After A lot of searching online i managed to get to a working solution in that matter! so for everyone else out there looking for the right answer
protected String doInBackground(Void... voids) {
StringBuilder sb = new StringBuilder();
try (Reader reader = new InputStreamReader(
new URL("https://play.google.com/store/apps/details?id="+BuildConfig.APPLICATION_ID+"&hl=en")
.openConnection()
.getInputStream()
, "UTF-8"
)) {
while (true) {
int ch = reader.read();
if (ch < 0) {
break;
}
sb.append((char) ch);
}
} catch (MalformedURLException ex) {
Log.d("ERROR", ex.getMessage());
return null;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.d("ERROR", e.getMessage());
return null;
} catch (IOException e) {
e.printStackTrace();
Log.d("ERROR", e.getMessage());
return null;
}
String parts[] = sb.toString().split("Current Version");
String res = parts[1].substring(
parts[1].indexOf("htlgb") + 7, parts[1].indexOf("htlgb") + 11
).trim().toString();
return res;
}
Accepted answer is not working any longer. Recently I had to change the code.
Try the following:
Jsoup.connect("https://play.google.com/store/apps/details?id=APPPACKAGE&hl=en")
.timeout(10000)
.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:contains(Current Version)").last().parent()
.select("span").last()
.ownText();
Basically, you should check the latest version of your app in the market and the version of the app on the device and decide if there is any update available. For doing this please try this:
You should use this for getting the current version (version of the app 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 for getting the latest version in the Google play (taken from https://stackoverflow.com/a/32942785/444991):
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 app starts, check them against each others like this:
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. But make sure you already imported jsoup library.
Extra: For importing jsoup library, follow these steps:
1- go to File menu
2- project structure
3- left side click on app
4- Choose dependencies tab
5- Click on +
6- Click on library dependency
7- Search "jsoup"
8- choose org.jsoup:jsoup and click ok
Adding a slight modification in @Mohammad answer, for current play page, below worked for me.
@Override
protected String doInBackground(String... params) {
try {
Document doc = Jsoup.connect("https://play.google.com/store/apps/details?id=YOUR_PACKAGE_NAME").get();
Element element = doc.getElementsByClass("BgcNfc").get(3);
latestVersion = element.parent().children().get(1).children().text();
} catch (Exception e) {
latestVersion = currentVersion;
}
return latestVersion;
}
***Its not a generic answer as it may change as play page make changes*
If you don't want to use a library like Jsoup, you can use something like this to get the current version number from 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 keep the "&hl=en" in the URL, the character encoding UTF-8 should be OK.