Skip to content

Instantly share code, notes, and snippets.

@gregmercer
Created July 5, 2017 15:58
Show Gist options
  • Save gregmercer/17638990efdf11039cf1f0929915e92a to your computer and use it in GitHub Desktop.
Save gregmercer/17638990efdf11039cf1f0929915e92a to your computer and use it in GitHub Desktop.
Xamarin Android - Checks if App needs to be updated, and if so takes the user to the Google Play Store.
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)));
}
}
private async Task<bool> NeedUpdate()
{
try
{
var curVersion = PackageManager.GetPackageInfo(PackageName, 0).VersionName;
var newVersion = curVersion;
string htmlCode;
using (HttpClient client = new HttpClient())
{
htmlCode = await client.GetStringAsync("https://play.google.com/store/apps/details?id=" + PackageName + "&hl=en");
}
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(htmlCode);
newVersion = doc.DocumentNode.SelectSingleNode("//div[@itemprop='softwareVersion']").InnerHtml.ToString();
return String.Compare(curVersion, newVersion) < 0;
}
catch (Exception e)
{
Toast.MakeText(
this,
"Trouble validating app version. Check your internet connection",
ToastLength.Long
).Show();
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment