11

当我的应用程序打开时,我想在 google play 上检查应用程序版本。如果应用程序的版本高于已安装的应用程序,我想通知用户更新应用程序。我从这里找到了“android-query”jar ,在这里我无法动态检查版本,我想设置主要、次要或修订版。任何人请帮助我该怎么办?

提前致谢

4

5 回答 5

13

基本上,您应该检查市场上您的应用程序的最新版本以及设备上的应用程序版本,并确定是否有可用的更新。为此,请尝试以下操作:

您应该使用它来获取当前版本(设备上的应用程序版本):

private String getCurrentVersion(){
PackageManager pm = this.getPackageManager();
PackageInfo pInfo = null;

        try {
            pInfo =  pm.getPackageInfo(this.getPackageName(),0);

        } catch (PackageManager.NameNotFoundException e1) {
            e1.printStackTrace();
        }
        String currentVersion = pInfo.versionName;

        return currentVersion;
    }

并使用它来获取 Google play 中的最新版本(取自https://stackoverflow.com/a/32942785/444991):

private class GetLatestVersion extends AsyncTask<String, String, String> {
String latestVersion;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            //It retrieves the latest version by scraping the content of current version from play store at runtime
            String urlOfAppFromPlayStore = "https://play.google.com/store/apps/details?id= your app package address";
            Document  doc = Jsoup.connect(urlOfAppFromPlayStore).get();
            latestVersion = doc.getElementsByAttributeValue("itemprop","softwareVersion").first().text();

        }catch (Exception e){
            e.printStackTrace();

        }

        return latestVersion;
    }
}

然后,当您的应用程序启动时,像这样检查它们:

String latestVersion = "";
        String currentVersion = getCurrentVersion();
        Log.d(LOG_TAG, "Current version = " + currentVersion);
        try {
            latestVersion = new GetLatestVersion().execute().get();
            Log.d(LOG_TAG, "Latest version = " + latestVersion);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        //If the versions are not the same
        if(!currentVersion.equals(latestVersion)){
            final AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("An Update is Available");
            builder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //Click button action
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=your app package address")));
                    dialog.dismiss();
                }
            });

            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //Cancel button action
                }
            });

            builder.setCancelable(false);
            builder.show();
        }

并显示用户更新对话框。但请确保您已经导入了 jsoup 库。

额外:要导入 jsoup 库,请按照下列步骤操作:

1- 转到文件菜单
2- 项目结构
3- 左侧单击应用程序
4- 选择依赖项选项卡
5- 单击 +
6- 单击库依赖项
7- 搜索“jsoup”
8- 选择 org.jsoup:jsoup 并单击确定

于 2015-11-25T19:36:07.107 回答
5

如果您不想使用 Jsoup 之类的库,可以使用类似这样的方法从 Google Play 获取当前版本号:

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;

public class StackAppVersion {

    public static void main(String[] args) {
        try {
            System.out.println(currentVersion());
        } catch (IOException ex) {
            System.out.println("Failed to read Google Play page!");
        }
    }

    private static String currentVersion() throws IOException {
        StringBuilder sb = new StringBuilder();

        try (Reader reader = new InputStreamReader(
                new URL("https://play.google.com/store/apps/details?id=com.stackexchange.marvin&hl=en")
                        .openConnection()
                        .getInputStream()
                , "UTF-8"
        )) {
            while (true) {
                int ch = reader.read();
                if (ch < 0) {
                    break;
                }
                sb.append((char) ch);
            }
        } catch (MalformedURLException ex) {
            // Can swallow this exception if your static URL tests OK.
        }

        String parts[] = sb.toString().split("softwareVersion");

        return parts[1].substring(
                parts[1].indexOf('>') + 1, parts[1].indexOf('<')
        ).trim();
    }
}

如果在 URL 中保留“&hl=en”,则字符编码 UTF-8 应该是可以的。

于 2016-05-15T01:32:07.657 回答
3

在@Mohammad 的答案中添加了一个轻微的修改,对于当前的播放页面,下面对我有用。

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

        try {
            Document doc = Jsoup.connect("https://play.google.com/store/apps/details?id=YOUR_PACKAGE_NAME").get();
            Element element = doc.getElementsByClass("BgcNfc").get(3);
            latestVersion = element.parent().children().get(1).children().text();
        } catch (Exception e) {
            latestVersion = currentVersion;
        }

        return latestVersion;
    }

***它不是一个通用的答案,因为它可能会随着播放页面的变化而改变*

于 2018-05-30T07:52:52.507 回答
2

接受的答案不再起作用。最近我不得不更改代码。

尝试以下操作:

Jsoup.connect("https://play.google.com/store/apps/details?id=APPPACKAGE&hl=en")
                .timeout(10000)
                .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:contains(Current Version)").last().parent()
                .select("span").last()
                .ownText();
于 2018-05-10T16:59:24.470 回答
0

经过大量在线搜索后,我设法找到了一个可行的解决方案!所以对于其他寻找正确答案的人来说

受保护的字符串 doInBackground(Void... voids) {

        StringBuilder sb = new StringBuilder();

        try (Reader reader = new InputStreamReader(
                new URL("https://play.google.com/store/apps/details?id="+BuildConfig.APPLICATION_ID+"&hl=en")
                        .openConnection()
                        .getInputStream()
                , "UTF-8"
        )) {
            while (true) {
                int ch = reader.read();
                if (ch < 0) {
                    break;
                }
                sb.append((char) ch);
            }
        } catch (MalformedURLException ex) {
            Log.d("ERROR", ex.getMessage());
            return null;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            Log.d("ERROR", e.getMessage());
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            Log.d("ERROR", e.getMessage());
            return null;
        }

        String parts[] = sb.toString().split("Current Version");
        String res = parts[1].substring(
                parts[1].indexOf("htlgb") + 7, parts[1].indexOf("htlgb") + 11
        ).trim().toString();
        return res;

    }
于 2018-04-03T13:51:14.177 回答