提问者:小点点

当用户打开应用程序时,是否有任何插件或方法显示“新版本的应用程序可用”警报


我有Xamarin.Android应用程序,我想向用户显示警报(当用户打开应用程序时),请下载最新版本。像其他应用程序一样。我该怎么做?

我尝试了几种方法,但没有成功。我尝试过这个。这与Android大不相同。我需要Xamarin。

我已经问过这个问题,但是由于Play商店策略发生了一些变化,现有功能出现了故障,因此我没有得到特定的字符串"itemprop=\"softwareVersion\"


共1个答案

匿名用户

工作答案。我已经使用插件为该Plugin. LatestVersionXam.Android从谷歌播放商店获得最新版本的应用程序。下面一行代码返回最新的应用程序版本。

var appStoreVersionString = await CrossLatestVersion.Current.GetLatestVersionNumber();

那么这就是进一步的实现

private void CompareVersion()
{
    double currentVersion = 0d;
    double appStoreversion = 0d;
    bool IsUpdateRequired = false;
    if (Context.PackageName != null)
    {
        PackageInfo info = Context.PackageManager.GetPackageInfo(Context.PackageName, PackageInfoFlags.Activities);
        string currentVersionStrig = info.VersionName;
        currentVersion = Convert.ToDouble(currentVersionStrig);
    }
    try
    {
        if (IsUpdateRequired == false)
        {
            string appStoreVersionString = string.Empty;
            if (CheckNetConnection.IsNetConnected())
            {
                Task.Run(async () => { appStoreVersionString = await CrossLatestVersion.Current.GetLatestVersionNumber();}).Wait();
                if (!string.IsNullOrEmpty(appStoreVersionString))
                {
                    appStoreversion = Convert.ToDouble(appStoreVersionString);
                    if ((appStoreversion.ToString() != currentVersion.ToString() && (appStoreversion > currentVersion)))
                    {
                        IsUpdateRequired = true;
                    }
                }
            }
        }
        if (IsUpdateRequired)
        {
            Activity.RunOnUiThread(() =>
            {
                AlertDialog dialog = null;
                var Alertdialog = new Android.App.AlertDialog.Builder(Context);
                Alertdialog.SetTitle("Update Available");
                Alertdialog.SetMessage($"A new version of [" + appStoreversion + "] is available. Please update to version [" + appStoreversion + "] now.");
                Alertdialog.SetNegativeButton("Cancel", (sender, e) =>
                {
                    if (dialog == null)
                    {
                        dialog = Alertdialog.Create();
                    }
                    dialog.Dismiss();
                });
                Alertdialog.SetPositiveButton("Update", (sender, e) =>
                {
                    string appPackage = string.Empty;
                    try
                    {
                        appPackage = Application.Context.PackageName;
                        Utilities.Logout(Activity);
                        var ints = new Intent(Intent.ActionView, Android.Net.Uri.Parse("market://details?id=" + appPackage));
                        ints.SetFlags(ActivityFlags.ClearTop);
                        ints.SetFlags(ActivityFlags.NoAnimation);
                        ints.SetFlags(ActivityFlags.NewTask);
                        Application.Context.StartActivity(ints);
                    }
                    catch (ActivityNotFoundException)
                    {
                        var apppack = Application.Context.PackageName;
                        Utilities.Logout(Activity);
                        var ints = new Intent(Intent.ActionView, Android.Net.Uri.Parse("market://details?id=" + appPackage));
                        ints.SetFlags(ActivityFlags.ClearTop);
                        ints.SetFlags(ActivityFlags.NoAnimation);
                        ints.SetFlags(ActivityFlags.NewTask);
                        Application.Context.StartActivity(ints);
                    }
                    //this kills the app? 
                    Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
                    System.Environment.Exit(1);
                });
                if (dialog == null)
                    dialog = Alertdialog.Create();
                dialog.Show();
            });
        }
    }
    catch (Exception ex)
    {
        var objLog = new LogService();
        objLog.MobileLog(ex, SISConst.UserName);
    }
}