Android get Google Play Store app version

前端 未结 8 1629
渐次进展
渐次进展 2021-02-06 11:49

I am using this code to get the Google Play Store app version but this is causing my app to hang. Please specify another way to get the app version or how I can use this code to

相关标签:
8条回答
  • 2021-02-06 12:29

    You need to put it into AysncTask

     //********* Check for Updated Version of APP
        private class GetVersionCode extends AsyncTask<Void, String, String> {
            String currentVersion = null;
    
            @Override
            protected String doInBackground(Void... voids) {
    
                String newVersion = null;
    
                try {
                    currentVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
                    newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + Activity.this.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) {
                    e.printStackTrace();
                }
                return null;
            }
    
            @Override
            protected void onPostExecute(String onlineVersion) {
                super.onPostExecute(onlineVersion);
                if (onlineVersion != null && !onlineVersion.isEmpty()) {
                    if (!currentVersion.equalsIgnoreCase(onlineVersion)) {
                        //Todo code here
    
                    }
                }
                Log.d("update", "Current version " + currentVersion + "playstore version " + onlineVersion);
            }
        }
    
    0 讨论(0)
  • 2021-02-06 12:34

    The problem arises because the element div [itemprop = softwareVersion] is no longer found on the Google Play website (therefore it no longer returns the version number), it should only be changed to a new query with the changes made by google on your website.

    Note: Take into account that if the structure is changed again, this code must be changed.

    I hope it helps

    Update: 12/09/2019

    public class VersionChecker extends AsyncTask<String, String, String> {
    
        @Override
        protected String doInBackground(String... params) {
    
            private String newVersion;
    
            try {
            Document document = Jsoup.connect("https://play.google.com/store/apps/details?id=" + "com.example.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();
            if (document != null) {
                Elements element = document.getElementsContainingOwnText("Current Version");
                for (Element ele : element) {
                    if (ele.siblingElements() != null) {
                        Elements sibElemets = ele.siblingElements();
                        for (Element sibElemet : sibElemets) {
                            newVersion = sibElemet.text();
                        }
                    }
                }
            }
            return newVersion;
        }
    }
    

    Codigo Java

    VersionChecker versionChecker = new VersionChecker();
    try {
        String latestVersion = versionChecker.execute().get();
        String versionName = BuildConfig.VERSION_NAME.replace("-DEBUG","");
        if (latestVersion != null && !latestVersion.isEmpty()) {
            if (!latestVersion.equals(versionName)) {
                showDialogToSendToPlayStore();
            }else{
                continueWithTheLogin();
            }
        }
        Log.d("update", "Current version " + Float.valueOf(versionName) + ", Playstore version " + Float.valueOf(latestVersion));
    
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    
    0 讨论(0)
提交回复
热议问题