簡體   English   中英

Android 獲取 Google Play Store 應用版本

[英]Android get Google Play Store app version

我正在使用此代碼獲取 Google 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();
}

只需替換您的代碼

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

                    

與以下:

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

它會工作..!

出現問題是因為在 Google Play 網站上不再找到元素div [itemprop = softwareVersion] (因此它不再返回版本號),只能將其更改為具有 google 在您網站上所做更改的新查詢.

注意:考慮到如果再次更改結構,則必須更改此代碼。

我希望它有幫助

更新: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;
    }
}

爪哇

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();
}
  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使用這種方法,您可以在需要時“觸發”更新,在您身邊有更多控制權(尤其是如果您想恢復“請更新”消息,如果您在發布的最新版本中發現錯誤,則很有用)

改變這兩行

{
 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()因為結果是由操作系統提供的,而不是構建參數中的常量。

你需要把它放到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);
        }
    }

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()].

試試這個

替換代碼,

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

您可以使用 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);

在您的 On Success 方法中,您可以調用

result.availableVersionCode()

當版本不不同時返回 0。 如果不同,您可以直接從您的應用程序中提取此值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM