0

I have Xamarin.Android app, I want to show alert to user (when user opens app) that please download latest version. Like other apps. How can I do that?

I have tried few ways but didn't get succeed. I have tried this. This is quite different from Android. I need it for Xamarin.

I already asked this question but existing functionality broke down due to having some changes in play store policies, consequently I am not getting specific string "itemprop=\"softwareVersion\">" from play store so I asked new question. Thank you

R15
  • 13,982
  • 14
  • 97
  • 173
  • I always assumed the App would do a web service call to it's home server to see if a new version had been released. – Richard Hubley Jan 31 '19 at 13:14
  • @RichardHubley - Yeah for sure. But the issue is existing working functionality broke down due to not finding this string `"itemprop=\"softwareVersion\">"`. According to link I have given in question. – R15 Jan 31 '19 at 13:17
  • You already asked this question, received an answer, and ACCEPTED the answer. If that answer was somehow inadequate you need to explain what specific problem you are having with it. – Jason Jan 31 '19 at 13:18
  • @ken-white is the same author. he's obviously having a new problem – Richard Hubley Jan 31 '19 at 13:20
  • @Jason - Existing working functionality broke down due to not finding this string `"itemprop=\"softwareVersion\">"` in `html` coming from _play store_. – R15 Jan 31 '19 at 13:20
  • @CGPA6.4 I added a new answer : https://stackoverflow.com/a/54463109/4984832 – SushiHangover Jan 31 '19 at 14:48

1 Answers1

0

Working answer. I have used plugin for that Plugin.LatestVersion for Xam.Android to get latest version of app from Google play store. below line of code was returning latest app version.

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

Then this is the further implementation

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);
    }
}
R15
  • 13,982
  • 14
  • 97
  • 173