【问题标题】:Xamarin.Forms popup "New Version Available"Xamarin.Forms 弹出“新版本可用”
【发布时间】:2017-02-18 13:25:44
【问题描述】:

我正在开发 Xamarin.forms Android 项目,我正在寻找为用户显示弹出窗口:

新版本可用

当用户尝试打开应用程序并且 Play-store 上有新的更新可用时。

【问题讨论】:

  • 你检查过曲棍球应用吗?
  • 我读过它..也许我会用它
  • 一开始为什么要这样做?如果您发布应用,商店不会自动更新到较新的版本吗?
  • 当然,但有些用户自动更新=false,在Play商店..
  • @MikeDarwish 重新考虑后,hockeyapp 不会通知用户来自应用商店的更新,所以我猜 Bill Reiss 的答案是您主要寻找的。如果您想显示您的应用程序已更新的弹出窗口,您可以检查您的网络服务并将其显示给用户,并通过一种方式将他重定向到商店中的应用程序。

标签: c# android xamarin.android xamarin.forms portable-class-library


【解决方案1】:

我认为最简单的方法是在您自己的服务器上拥有一个返回当前版本号的 Web 服务,不幸的是,您每次在商店中更新应用程序时都需要更新此版本号。

【讨论】:

    【解决方案2】:

    在 GitHub Gist 帐户中创建具有最新版本号的文本文件。

    获取原始网址

    字符串 url = "https://gist.githubusercontent.com/YOUR_ACCOUNT_NAME/0df1fa45aa11753de0a85893448b22de/raw/UpdateInfo.txt";

    private static async Task<string> GetLatestVersion(string URL)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(URL));
            request.ContentType = "application/json"; //i am using a json file 
            request.Method = "GET";
            request.Timeout = 20000;
            // Send the request to the server and wait for the response:
            try
            {
                using (WebResponse response = await request.GetResponseAsync())
                {
                    // Get a stream representation of the HTTP web response:
                    using (Stream stream = response.GetResponseStream())
                    {                    
                        StreamReader reader = new StreamReader(stream);
                        return reader.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }
    

    这将返回您应用的最新版本。并检查活动中现有的应用程序版本

    var versionName = Application.Context.ApplicationContext.PackageManager.GetPackageInfo(Application.Context.ApplicationContext.PackageName, 0).VersionName;
            var currentVer = double.Parse(versionName);
    

    但您必须在任何时候在 Play 商店中更新应用时更新此版本号。

    【讨论】:

    • 很好的解决方案。但我还是更喜欢动态解决方案
    • @MikeDarwish:什么是动态解决方案?
    【解决方案3】:
    private class GetVersionCode extends AsyncTask<Void, String, String> {
        @Override
        protected String doInBackground(Void... voids) {
    
            String newVersion = null;
            try {
                newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + SplashActivity.this.getPackageName() + "&hl=it")
                        .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[itemprop=softwareVersion]")
                        .first()
                        .ownText();
                 return newVersion;
            } catch (Exception e) {
                return newVersion;
            }
        }
    
        @Override
        protected void onPostExecute(String onlineVersion) {
            super.onPostExecute(onlineVersion);
            String currentVersion = null;
            try {
                currentVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
            if (onlineVersion != null && !onlineVersion.isEmpty()) {
                if (Float.valueOf(currentVersion) < Float.valueOf(onlineVersion)) {
    
                        final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(SplashActivity.this);
                        alertDialogBuilder.setTitle("Product Update");
                        alertDialogBuilder.setMessage("A new version is available. Would you like to Upgrade now? (Current: "+currentVersion+" Latest: "+onlineVersion+")");
                        alertDialogBuilder.setPositiveButton("ok",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface arg0, int arg1) {
                                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+SplashActivity.this.getPackageName())));
                                    }
                                });
    
                        alertDialogBuilder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                alertDialogBuilder.setCancelable(true);
                                finish();
                                loginUserCheck();
                            }
                        });
    
                        AlertDialog alertDialog = alertDialogBuilder.create();
                        alertDialog.show();
                 }
    

    【讨论】:

    • 能否添加更多描述
    • 您只需从 Playstore 获取您应用的新版本并检查您的当前版本是否大于应用版本,如果更高,则显示弹出窗口
    猜你喜欢
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 2017-08-06
    • 1970-01-01
    • 2017-09-18
    • 2019-05-15
    相关资源
    最近更新 更多