Friday, 4 August 2017

Getting App Version Code of PlayStore


Add given dependency in your gradle file-

compile 'org.jsoup:jsoup:1.8.3'

and add this code in your Activity or Fragment

private class GetVersionCode extends AsyncTask<Void, String, String> {
    @Override    protected String doInBackground(Void... voids) {

        String newVersion = null;
        try {
            newVersion = Jsoup.connect("https://play.google.com/store/apps/
details?id="+ getActivity().getPackageName() + "&hl=it")
                    .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();
            return newVersion;
        } catch (Exception e) {
            return newVersion;
        }
    }

    @Override    protected void onPostExecute(String onlineVersion) {
        super.onPostExecute(onlineVersion);
        if (onlineVersion != null && !onlineVersion.isEmpty()) {
            if (Float.valueOf(currentVersion) < Float.valueOf(onlineVersion)) {
                showDialog();
            }
        }
    }
}


currentVersion = getActivity().getPackageManager().getPackageInfo(getActivity()
.getPackageName(), 0).versionName;
GetVersionCode ver = new GetVersionCode();
ver.execute();

No comments:

Post a Comment