Android get Google Play Store app version

前端 未结 8 1631
渐次进展
渐次进展 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 {
            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);
            }
        }
    

提交回复
热议问题