Android获得Play商店应用版本

时间:2018-02-22 12:04:32

标签: android google-play version

我使用此代码获取Play商店应用版本,但这导致我的应用挂起。请指定其他方式来获取Play商店应用程序版本或如何使用此代码使应用程序不挂起并成功运行。提前谢谢。

    public class VersionChecker extends AsyncTask<String, String, String> {

    private String newVersion;

    @Override
    protected String doInBackground(String... params) {

        try {
            newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + "trai.gov.in.dnd" + "&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()
                    .select("div[itemprop=softwareVersion]")
                    .first()
                    .ownText();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return newVersion;
    }
}

这是我从asynctask获取版本的代码

VersionChecker versionChecker = new VersionChecker(); 
try 
{ String appVersionName = BuildConfig.VERSION_NAME; 
String mLatestVersionName = versionChecker.execute().get(); 
if (versionChecker.compareVersionNames(appVersionName, mLatestVersionName) == -1) 
{ 
showAppUpdateDialog(); 
} 
} catch (InterruptedException | ExecutionException e) { 
e.printStackTrace(); 
} 

8 个答案:

答案 0 :(得分:4)

只需替换您的代码

                    .get()
                    .select("div[itemprop=softwareVersion]")
                    .first()
                    .ownText();

以下:

                   .get()
                   .select(".hAyfc .htlgb")
                   .get(3)
                   .ownText();

它会起作用..!

答案 1 :(得分:0)

更改这两行

{
 newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + "package name" + "&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")
....

您使用的是BuildConfig.APPLICATION_ID但需要package_name

BuildConfig由Gradle提供。如果您未使用Gradle构建,则无法使用BuildConfig访问程序包名称。

我会使用Context.getPackageName(),因为结果是从操作系统提供的,而不是构建参数中的常量。

答案 2 :(得分:0)

您需要将其放入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);
        }
    }

答案 3 :(得分:0)

问题出现是因为Google Play网站上找不到元素div [itemprop = softwareVersion](因此它不再返回版本号),只应将其更改为新的查询,其中包含谷歌所做的更改你的网站。

注意:考虑到如果再次更改结构,则必须更改此代码。

我希望它有所帮助

更新:30/04/2019

public class VersionChecker extends AsyncTask<String, String, String> {

    private String newVersion;

    @Override
    protected String doInBackground(String... params) {

        try {
            newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + "trai.gov.in.dnd" + "&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()
                    .select(".IxB2fe .hAyfc:nth-child(4) .htlgb span")
                    .get(0)
                    .ownText();
        } catch (IOException e) {
            e.printStackTrace();
        }
        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();
}

答案 4 :(得分:0)

Google将API从'softwareVersion'更改为'currentVersion',我们可以使用“.hAyfc .htlgb”获得相同的版本

下面的代码不起作用,即

latestVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + context.getPackageName() + "&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().select("div[itemprop=softwareVersion]").first().ownText();

而不是上面的代码只需添加此代码,它将正常工作,即

Document document = Jsoup.connect("https://play.google.com/store/apps/details?id=" + context.getPackageName() + "&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) {
        if(document.select(".hAyfc .htlgb").get(5) != null) {
            latestVersion = document.select(".hAyfc .htlgb").get(5).ownText();
        } else {
            latestVersion = currentVersion;
        }
    }

Note: There is no official API to get the current application version from Google Play. So use the above code to get the current version. Sometimes get(5).ownText() will change as google is updating the Google Play site. [Previously it was working with get(2).ownText()].

答案 5 :(得分:0)

试试吧,

替换代码,

                .get()
                .select("div:containsOwn(Current Version)")
                .next()
                .text();

答案 6 :(得分:0)

您可以使用UpdateManager轻松实现这一目标-https://developer.android.com/reference/com/google/android/things/update/UpdateManager

您可以这样设置更新管理器

// Creates instance of the manager.
AppUpdateManager appUpdateManager = AppUpdateManagerFactory.create(this);

// Returns an intent object that you use to check for an update.
Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();

//On Success Listener
appUpdateInfoTask.addOnSuccessListener(this);

在成功方法中,您可以致电

result.availableVersionCode()

当版本相同时,哪个将返回0。如果不同,您可以直接从应用程序中提取此值。

答案 7 :(得分:0)

  1. 似乎您使用了Google Play商店网站上显示的“应用程序版本”来触发用户的“更新弹出窗口”,并在有新版本可用时通知您。首先,Google Play商店网站经常更改,因此不太可靠,因为您的应用程序可能无法“解析/处理”这些更改。如果您想保留这种方法,其他答案已经说明了如何解析正确的HTML标记以及如何在后台正确执行。请记住,您的应用仍将负责“解析”,最终您将在网络服务器上进行远程解析,并向您的应用公开一个稳定的API终结点,以检索您自己应用的“最新版本”。如果您不想解析HTML标记或托管自己的API,可以使用第三方API来完成。我们在此处提供了一个这样的API:https://42matters.com/docs/app-market-data/android/apps/lookup(它返回具有应用程序最新“版本名称”的JSON对象)。

  2. 您也可以使用https://github.com/danielemaddaluno/Android-Update-Checker来不实现自己的代码,因为它的功能与您差不多。

  3. 我个人会使用Firebase Remote Config https://firebase.google.com/docs/remote-config之类的远程服务,或者是托管在您网站上的简单JSON文件,用于指定您应用的最新发布版本(请记住,一旦更新了您的应用该应用程序确实已在Google Play上发布,如今可能需要一段时间。另外,我宁愿使用“ versionCode”,如此处所述:https://developer.android.com/studio/publish/versioning 通过这种方法,您可以在需要时“触发”更新,并获得更多控制权(特别是在您要还原“请更新”消息的情况下,如果发现发布的最新版本的错误很有用)