获取谷歌Play 里APP的版本号

谷歌Play没有提供Api来获取App的版本号。

但是有个 https://play.google.com/store/apps/details?id=com.xxx.xxx,id是包名。

那就只能用Jsoup来解析网页,从而获取谷歌Play展示的版本号,Jsoup 的 jar包下载地址https://jsoup.org/download

public class CheckVersionAsyncTask extends AsyncTask{
    Context context;
    String currentVersion;
    String oldVersion;
    private String latestVersion;

    public CheckVersionAsyncTask(Context context, String oldVersion) {
        this.context = context;
        this.oldVersion = oldVersion;
    }


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

        try {
            latestVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + context.getPackageName())
                    .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();
            Log.e("latestversion","---"+latestVersion);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return latestVersion;

    }

    @Override
    protected void onPostExecute(String latestVersion) {
        if(latestVersion!=null){
            if(!latestVersion.equalsIgnoreCase(oldVersion)){
                 Toast.makeText(context,"需要更新咯",Toast.LENGTH_LONG).show();

            }else {
                Toast.makeText(context,"一样的",Toast.LENGTH_LONG).show();
            }
        }
        super.onPostExecute(latestVersion);

    }

}

这个网页不是不变的,所以个人建议不可取,那怎么办呢??????????

获取谷歌Play 里APP的版本号_第1张图片

你可能感兴趣的:(获取谷歌Play 里APP的版本号)