Android应用程式内版本检查 [英] Android In-app version check

查看:145
本文介绍了Android应用程式内版本检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用打开时检查Google Play上的应用版本.如果应用程序的版本高于已安装的应用程序,我想通知用户更新该应用程序.我从此处找到了"android-query"罐,在此我无法检查版本动态地以此为基础,我想设置主要",次要"或修订".有人请帮我怎么办?

I want to check application version on google play when my app open. If App has higher version than the installed app, I want to notify user to update the app. I found that "android-query" jar from here, in this I can't check version dynamically on this, I suppose to set Major, Minor or Revision. Anyone please help me how can I do?

预先感谢

推荐答案

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

Basically, you should check the latest version of your app in the market and the version of the app on the device and decide if there is any update available. For doing this please try this:

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

You should use this for getting the current version (version of the app on the device):

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 ):

And use this for getting the latest version in the Google play (taken from 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;
    }
}

然后,当您的应用启动时,像这样相互检查:

Then, when your app starts, check them against each others like this:

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库.

And show the user update dialog. But make sure you already imported jsoup library.

其他:要导入jsoup库,请执行以下步骤:

Extra: For importing jsoup library, follow these steps:

1-转到文件"菜单
2-项目结构
3-左侧点击应用
4-选择依赖项选项卡
5-单击+
6-单击库依赖
7-搜索"jsoup"
8-选择org.jsoup:jsoup,然后单击确定

1- go to File menu
2- project structure
3- left side click on app
4- Choose dependencies tab
5- Click on +
6- Click on library dependency
7- Search "jsoup"
8- choose org.jsoup:jsoup and click ok

这篇关于Android应用程式内版本检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆