Android :从Google play 获取当前APP 版本号

编程入门 行业动态 更新时间:2024-10-27 17:17:15

APP自动更新 可以从服务器下载更新,也可以跳转商店更新,今天说的是获取当前应用在Google play中的最新版本和本地版本比较,判断是否需要更新。

1 添加依赖

implementation 'org.jsoup:jsoup:1.10.2'

如果依赖包导入失败 可以改成 1.5.2
或者手动下载 https://jar-download/artifacts/org.jsoup/jsoup/1.10.2/source-code

2 创建工具类 VersionChecker

public class VersionChecker extends AsyncTask<String, String, String> {

     private String newVersion;

    @Override
    protected String doInBackground(String... params) {

        try {
            Document document = Jsoup.connect("https://play.google/store/apps/details?id=" + BuildConfig.APPLICATION_ID + "&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")
                    .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();
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return newVersion;
    }
}

3 使用:

VersionChecker versionChecker = new VersionChecker();
try {
     mLatestVersionName = versionChecker.execute().get();
     if (Double.parseDouble(BuildConfig.VERSION_NAME) < Double.parseDouble(mLatestVersionName)) {
      //perform your task here like show alert dialogue "Need to upgrade app"
      Intent intent = new Intent(Intent.ACTION_VIEW);
      intent.setData(Uri.parse("market://details?id=" + this.getPackageName()));
      if (intent.resolveActivity(getPackageManager()) != null) { //可以接收 
        startActivity(intent); 
      } else { //没有应用市场,我们通过浏览器跳转到Google Play 
        intent.setData(Uri.parse(“https://play.google/store/apps/details?id=” + this.getPackageName())); 
        startActivity(intent);
      }
} catch (InterruptedException | ExecutionException e) {
     e.printStackTrace();
}

更多推荐

Android :从Google play 获取当前APP 版本号

本文发布于:2023-06-13 10:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1378643.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:版本号   Google   Android   APP   play

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!