如何更新Play商店应用程序,即如果用户使用旧版本应用程序,我如何从Google Play商店获取应用程序版本信息以提示用户强制/建议更新应用程序.我已经通过了andorid-market-api,这不是官方的方式,也需要谷歌的oauth登录验证.我也经历了android查询 ,它提供了应用内版本检查,但它在我的情况下不起作用.我找到了以下两种选择:
使用将存储版本信息的服务器API
使用谷歌标签并在应用程序中访问它,这不是一个首选的方式.
还有其他方法可以轻松完成吗?
我建议不要使用库只是创建一个新类
1.
public class VersionChecker extends AsyncTask{ String newVersion; @Override protected String doInBackground(String... params) { try { 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") .referrer("http://www.google.com") .get() .select("div.hAyfc:nth-child(4) > span:nth-child(2) > div:nth-child(1) > span:nth-child(1)") .first() .ownText(); } catch (IOException e) { e.printStackTrace(); } return newVersion; }
在您的活动中:
VersionChecker versionChecker = new VersionChecker(); String latestVersion = versionChecker.execute().get();
就这些
这是jQuery版本,以获取版本号,如果其他人需要它.
$.get("https://play.google.com/store/apps/details?id=" + packageName + "&hl=en", function(data){ console.log($('').html(data).contents().find('div[itemprop="softwareVersion"]').text().trim()); });
使用此代码完美正常工作.
public void forceUpdate(){ PackageManager packageManager = this.getPackageManager(); PackageInfo packageInfo = null; try { packageInfo =packageManager.getPackageInfo(getPackageName(),0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } String currentVersion = packageInfo.versionName; new ForceUpdateAsync(currentVersion,TodayWork.this).execute(); } public class ForceUpdateAsync extends AsyncTask{ private String latestVersion; private String currentVersion; private Context context; public ForceUpdateAsync(String currentVersion, Context context){ this.currentVersion = currentVersion; this.context = context; } @Override protected JSONObject doInBackground(String... params) { try { 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.hAyfc:nth-child(3) > span:nth-child(2) > div:nth-child(1) > span:nth-child(1)") .first() .ownText(); Log.e("latestversion","---"+latestVersion); } catch (IOException e) { e.printStackTrace(); } return new JSONObject(); } @Override protected void onPostExecute(JSONObject jsonObject) { if(latestVersion!=null){ if(!currentVersion.equalsIgnoreCase(latestVersion)){ // Toast.makeText(context,"update is available.",Toast.LENGTH_LONG).show(); if(!(context instanceof SplashActivity)) { if(!((Activity)context).isFinishing()){ showForceUpdateDialog(); } } } } super.onPostExecute(jsonObject); } public void showForceUpdateDialog(){ context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + context.getPackageName()))); } }
Firebase Remote Config在这里可以提供最佳帮助,
请参考此答案 /sf/ask/17360801/