2018年5月如何通过编程检查Google Play Store应用更新?
嘿,我懂你现在的困扰——原来靠jsoup抓取softwareVersion字段检查Play Store更新的方法,在2018年5月Google调整网页结构后彻底失效了。别慌,我给你整理了几个靠谱的替代方案,从官方推荐到灵活自定义的都有:
方案一:使用Google官方In-App Updates API(首推)
Google在2018年前后推出了这个官方API,不仅能帮你检查更新,还能直接触发应用内的更新流程,完全符合Play Store的政策,也不用担心网页结构变动导致失效。
步骤如下:
- 先在项目的
build.gradle中添加依赖(对应2018年前后的稳定版本,可根据实际需求调整):
dependencies { implementation 'com.google.android.play:core:1.10.3' }
- 然后在代码中实现更新检查逻辑:
// 创建AppUpdateManager实例 AppUpdateManager appUpdateManager = AppUpdateManagerFactory.create(context); // 发起更新信息检查请求 Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo(); appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> { // 判断是否有可用更新,以及是否允许指定的更新类型 if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) { // 触发更新流程,这里以强制更新为例 try { appUpdateManager.startUpdateFlowForResult( appUpdateInfo, AppUpdateType.IMMEDIATE, this, 1001); // 自定义请求码,用于后续的onActivityResult回调 } catch (IntentSender.SendIntentException e) { e.printStackTrace(); } } });
方案二:自定义后端存储版本号(灵活可控)
如果不想依赖Google的API,你可以自己搭建一个简单的后端,或者用静态存储服务(比如Firebase Realtime Database、OSS静态文件)来存储应用的最新版本号和更新说明。这种方式完全由你掌控,不会受Google调整的影响。
举个例子:
- 后端存储的JSON文件内容:
{ "latestVersion": "2.1.0", "updateDesc": "修复了若干bug,优化了用户体验", "playStoreUrl": "https://play.google.com/store/apps/details?id=com.your.package.name" }
- 应用内通过网络请求获取并对比版本号(示例用OkHttp):
// 初始化OkHttp客户端 OkHttpClient client = new OkHttpClient(); // 构建请求 Request request = new Request.Builder() .url("https://your-storage-url.com/latest-version.json") .build(); // 发起异步请求 client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful() && response.body() != null) { String jsonContent = response.body().string(); JSONObject jsonObject = new JSONObject(jsonContent); String latestVersion = jsonObject.getString("latestVersion"); // 获取当前应用版本号 String currentVersion = BuildConfig.VERSION_NAME; // 对比版本号(这里实现了一个简单的版本号比较逻辑) if (compareVersionNumbers(currentVersion, latestVersion) < 0) { // 有可用更新,在主线程弹出提示 runOnUiThread(() -> { new AlertDialog.Builder(MainActivity.this) .setTitle("发现新版本") .setMessage(jsonObject.getString("updateDesc")) .setPositiveButton("前往更新", (dialog, which) -> { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse(jsonObject.getString("playStoreUrl"))); startActivity(intent); }) .setNegativeButton("稍后再说", null) .show(); }); } } } }); // 版本号比较方法,支持多段式版本号(如1.2.3 vs 1.3.0) private int compareVersionNumbers(String current, String latest) { String[] currentParts = current.split("\\."); String[] latestParts = latest.split("\\."); int maxLength = Math.max(currentParts.length, latestParts.length); for (int i = 0; i < maxLength; i++) { int currentNum = i < currentParts.length ? Integer.parseInt(currentParts[i]) : 0; int latestNum = i < latestParts.length ? Integer.parseInt(latestParts[i]) : 0; if (currentNum != latestNum) { return currentNum - latestNum; } } return 0; }
方案三:爬取Play Store网页(备选,易失效)
如果实在想沿用网页抓取的方式,2018年5月之后Play Store网页的版本号元素位置发生了变化,你可以调整jsoup的选择器来定位。不过这种方法非常不稳定,Google随时可能再次调整网页结构,甚至触发反爬机制,所以只作为备选。
示例代码(适配2018年5月前后的网页结构):
// 模拟浏览器请求头,避免被拦截 Document doc = Jsoup.connect("https://play.google.com/store/apps/details?id=com.your.package.name") .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36") .get(); // 定位版本号元素(当时的结构是在class为"hAyfc"的div下的第二个span标签) Elements versionElements = doc.select("div.hAyfc span:nth-child(2)"); if (!versionElements.isEmpty()) { String latestVersion = versionElements.get(0).text(); // 后续版本对比逻辑同方案二 }
内容的提问来源于stack exchange,提问作者Flummox uses codidact.com




