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

时间:2019-01-31 13:12:04

标签: xamarin xamarin.android

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

我尝试了几种方法,但没有成功。我已经尝试过this。这与Android完全不同。我需要Xamarin。

我已经问了这个问题,但是由于 play store 策略中的某些更改,现有的功能崩溃了,因此我没有从"itemprop=\"softwareVersion\">"获得特定的字符串play store,所以我问新问题。谢谢

1 个答案:

答案 0 :(得分:1)

有效答案。我已使用Xam.Android的 Plugin.LatestVersion 插件从 Google play store 获取最新版本的应用。下面的代码行返回了最新的应用程序版本。

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);
    }
}