繁体   English   中英

如何获取 Android Play Store 的当前应用版本?

[英]How to get the current app version of Android Play Store?

我检查了谷歌或堆栈溢出的当前信息。 但是现在存在的代码不适用于我的项目。

try {
    val curVersion: String = "1.0.5"
    var newVersion: String? = curVersion
    newVersion =
        Jsoup.connect("https://play.google.com/store/apps/details?id=" + "com.lsn.lsn_communication_app")
            .timeout(3000)
            .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()
    if((curVersion) == (newVersion)){
        storeVersion = "1.0.5"
    }else{
        storeVersion = "1.0.0"
    }

} catch (e: Exception) {
    e.printStackTrace()
    storeVersion = ""
}

return storeVersion

这是我测试版本的代码。 有谁知道如何以除此之外的任何方式获取当前 Android 应用程序版本的 Play 商店吗?

我的项目正在使用 Retrofit 2 在存储库文件中异步运行 function。

如果您想知道 Google Play 中可用的版本,以便在有新更新可用时通知用户。 建议使用 应用内更新 API

     val appUpdateManager = AppUpdateManagerFactory.create(context)
    
    // Returns an intent object that you use to check for an update.
    val appUpdateInfoTask = appUpdateManager.appUpdateInfo
    
    // Checks that the platform will allow the specified type of update.
    appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
        if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
            // This example applies an immediate update. To apply a flexible update
            // instead, pass in AppUpdateType.FLEXIBLE
            && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
        ) {
            // Request the update.
           appUpdateManager.startUpdateFlowForResult(
          // Pass the intent that is returned by 'getAppUpdateInfo()'.
          appUpdateInfo,
         // Or 'AppUpdateType.FLEXIBLE' for flexible updates.
        AppUpdateType.IMMEDIATE,
        // The current activity making the update request.
          this,
        // Include a request code to later monitor this update request.
        MY_REQUEST_CODE)
      }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM