如何以编程方式获取实时应用程序版本?



最初,为了以编程方式获取实时应用程序版本,我使用了以下代码片段

try {
newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + appPackageName + "&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(".hAyfc .htlgb")
.get(7)
.ownText();
} catch (IOException e) {
e.printStackTrace();
}

但是在2022年5月底,这个代码片段开始抛出异常。有人有类似的问题吗?任何修复都是非常欢迎的。

* * *解决方案* * *

Google现在提供官方解决方案

<<blockquote>使用em> AppUpdateManager

来自官方文档

检查更新可用性

在请求更新之前,您需要首先检查是否存在更新要检查更新,使用AppUpdateManager,如下所示:

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

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

// Checks that the platform will allow the specified type of update. appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
// For a flexible update, use AppUpdateType.FLEXIBLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
// Request the update.
} }); 

结果包含更新可用性状态。如果更新是可用并且允许更新,返回的AppUpdateInfo也包含启动更新的意图。请参阅下一节了解如何启动更新

如果应用内更新已经在进行中,结果也会报告正在进行的更新的状态。

这对我有用。希望能有所帮助!

是的,google更新了他们的play store网站,这个解决方案不再有效。dom结构已经改变,应用版本不再隐藏在动态加载的模态后面。我目前也在努力寻找一个新的解决方案。

最新更新