Android get Google Play Store app version

前端 未结 8 1646
渐次进展
渐次进展 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:13

    You can achieve this much easier using UpdateManager - https://developer.android.com/reference/com/google/android/things/update/UpdateManager

    You can setup your Update Manager as such

    // 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);
    

    In your On Success method, you can call

    result.availableVersionCode()
    

    Which returns 0 when the version is not different. Which if it is different you can just pull this value from your app directly.

    0 讨论(0)
  • 2021-02-06 12:15

    Google change the API from 'softwareVersion' to 'currentVersion' and we can get the same version using ".hAyfc .htlgb"

    Below code will not work i.e.

    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();
    

    Instead of above code just add this code, it will work fine i.e.

    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()].
    
    0 讨论(0)
  • 2021-02-06 12:21

    Change this two lines

    {
     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")
    ....
    

    you are using BuildConfig.APPLICATION_ID but you need package_name

    BuildConfig is provided by Gradle. If you are not building using Gradle then you cannot access the package name using BuildConfig.

    I would use Context.getPackageName() because the result is provided from the operating system, rather than a constant in build parameters.

    0 讨论(0)
  • 2021-02-06 12:22
    1. It seems you use the "app-version" shown on the Google Play Store website to trigger an "update popup" to your users and notify when a new version is available. First of all, Google Play Store website is changing often, so that's not very reliable as your app might not be able to "parse/handle" those changes. If you want to keep that approach, some other answers already explained how to parse the right HTML tag and how to do in the background correctly. Keep in mind your app will be still responsible for the "parsing", eventually you would do the parsing remotely on a web-server, and expose to your app a stable API endpoint to retrieve the "latest-version" of your own app. If you do not want to bother with parsing HTML tags or host your own API, there are third-party APIs that can do that for you. We provide one such API here: https://42matters.com/docs/app-market-data/android/apps/lookup (it returns a JSON object with the latest "version name" of the app).

    2. You could also use https://github.com/danielemaddaluno/Android-Update-Checker to not implement your own code, under the hood it does more or less the same as you do.

    3. Personally I would use a remote service like Firebase Remote Config https://firebase.google.com/docs/remote-config or a simple JSON file hosted on your website specifying the latest published version of your app (just remember to change it once the update of your app is really published on Google Play, nowadays it might take a while). Also, I would rather use the "versionCode" as explained here: https://developer.android.com/studio/publish/versioning With this approach you could "trigger" the update when you want, more control on your side (especially in case you want to revert the "Please update" message, useful if you find a bug in the latest version you published)

    0 讨论(0)
  • 2021-02-06 12:24

    just try this,

    Replace the code ,

                    .get()
                    .select("div:containsOwn(Current Version)")
                    .next()
                    .text();
    
    0 讨论(0)
  • 2021-02-06 12:27

    Just replace your code

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

    with below:

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

    it will work..!

    0 讨论(0)
提交回复
热议问题