在Xamarin中查找版本并强制进行更新 [英] Looking up version and forcing an update in xamarin

查看:382
本文介绍了在Xamarin中查找版本并强制进行更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多关于在Java中执行此操作的文章,但是我发现NSoup(JSoup库的端口)对我不起作用,因此我无法将其移植到c#/Xamarin.对于我正在开发的游戏的多人游戏功能,我需要在开始多人游戏配对之前确保客户端已同步.这意味着我必须强迫用户更新应用程序(如果有可用的新版本),然后才允许他们邀请其他玩家参加比赛,加入快速比赛等.

There are many posts about doing this in java, but I found that NSoup (the port of the JSoup library) doesn't work for me, so I failed to port it to c#/Xamarin. For multiplayer functions of a game I'm working on, I need to make sure clients are synced before starting multiplayer matchmaking. This means I have to force the user to update the app if there's a new version available before they're allowed to invite other players to matches, join quick matches, etc..

例如,当用户按下快速匹配"按钮时,我需要:

So when a user presses the "quick match" button, for example, I need to:

  1. 检查版本名称(立即添加版本名称,而不是代码,以进行重大更改)
  2. 将版本名称与已安装的当前版本名称进行比较

  1. Check for the version name (im incrementing version name, not code, for breaking changes)
  2. Compare the version name from that to the current version name installed

3.

-如果新版本的名称大于当前版本的名称,则我需要为用户提供更新其应用程序的选项,如果他们选择是",则将其发送到我的应用程序的google play商店页面.然后,我让他们从那里更新,我们的工作就完成了.

-If the newer version name is greater than the current one, I need to give the user the option to update their app, and send them to the google play store page for my app if they choose 'yes'. Then I'll just let them update from there and our work is done.

-如果版本相同,则允许按钮的任何功能(即将其发送到等候室进行配对)

-If the versions are the same, allow whatever the button's functionality (i.e sending them to the waiting room for matchmaking) to proceed.

推荐答案

创建检查更新并采取相应措施所必需的方法:

Create the methods necessary to check for updates and act accordingly:

private void CheckUpdate(Action doIfUpToDate)
    {
        if(NeedUpdate())
        {
            Android.App.AlertDialog.Builder alert = new Android.App.AlertDialog.Builder(this);
            alert.SetTitle("New Update");
            alert.SetMessage("You must download the newest version of this to play multiplayer.  Would you like to now?");
            alert.SetCancelable(false);
            alert.SetPositiveButton("Yes", new EventHandler<DialogClickEventArgs>((object sender, DialogClickEventArgs e) => GetUpdate()));
            alert.SetNegativeButton("No", delegate{});
            alert.Show();
        }
        else
        {
            doIfUpToDate.Invoke();
        }
    }

private bool NeedUpdate()
    {
        try
        {
            var curVersion = PackageManager.GetPackageInfo(PackageName, 0).VersionName;
            var newVersion = curVersion;

            string htmlCode;
            //probably better to do in a background thread
            using (WebClient client = new WebClient())
            {
                htmlCode = client.DownloadString("https://play.google.com/store/apps/details?id=" + PackageName + "&hl=en");
            }

            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(htmlCode);

            newVersion = doc.DocumentNode.SelectNodes("//div[@itemprop='softwareVersion']")
                              .Select(p => p.InnerText)
                              .ToList()
                              .First()
                              .Trim();

            return String.Compare(curVersion, newVersion) < 0;
        }
        catch (Exception e)
        {
            Log.Error(TAG, e.Message);
            Toast.MakeText(this, "Trouble validating app version for multiplayer gameplay.. Check your internet connection", ToastLength.Long).Show();
            return true;
        }
    }

private void GetUpdate()
    {
        try
        {
            StartActivity(new Intent(Intent.ActionView, Android.Net.Uri.Parse("market://details?id=" + PackageName)));
        }
        catch (ActivityNotFoundException e)
        {
            //Default to the the actual web page in case google play store app is not installed
            StartActivity(new Intent(Intent.ActionView, Android.Net.Uri.Parse("https://play.google.com/store/apps/details?id=" + PackageName)));
        }
    }

然后从可以启动多人游戏的给定按钮开始:

And then from a given button that could start a multiplayer game:

var quickMatchButton = FindViewById<Button>(Resource.Id.button_quick_game);
quickMatchButton.Click += new EventHandler((object sender, EventArgs e) => CheckUpdate(() => startQuickMatch()));

这篇关于在Xamarin中查找版本并强制进行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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