简体   繁体   中英

How to open Google play books from a button click using Xamarin.Forms?

I am working on a app in Xamarin.Forms that has a button for books. I want it to open iBooks if the OS is iOS and Google play books if it is android

I have the iBooks Part down but How would I open google play books from the button click in the android version of the app?

Heres my xaml code:

<StackLayout Orientation="Horizontal" MinimumHeightRequest="30" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
              <Button  Image="books.png" HorizontalOptions="CenterAndExpand" BackgroundColor="Transparent" Clicked="OpenBooks" />
              <Button  Image="settings.png" HorizontalOptions="EndAndExpand" BorderColor="Transparent" BackgroundColor="Transparent" Clicked="gotosettings" />
          </StackLayout>

Heres My C# code:

public void OpenBooks(object sender, EventArgs e)
{
    switch (Device.OS)
    {
        case TargetPlatform.iOS:
            Device.OpenUri(new Uri("itms-books"));
            break;
            case TargetPlatform.Android:
            //open google play books code here
            break;
     }
}

any help would be amazing!

Thank in advance!

In the android platform you should know the package name of google play books and check if it is installed already.

In the PCL part you can not achieve it by Device.OpenUri("packagename");

You should use the dependency service to open the google play books app, and The google play books app package name is com.google.android.apps.books :

In the PCL side define the interface:

namespace OpenBooks_Demo
{
    public interface OpenBookInterface
    {
        void openBooks();
    }
}

In the andorid side implements the interface:

[assembly: Xamarin.Forms.Dependency(typeof(OpenBookImp))]
namespace OpenBooks_Demo.Droid
{
    public class OpenBookImp :  Java.Lang.Object, OpenBookInterface
    {
        public OpenBookImp() { }
        public void openBooks()
        {
            var ctx = Forms.Context;

            Intent launchIntent = new Intent();
            launchIntent = ctx.PackageManager.GetLaunchIntentForPackage("com.google.android.apps.books");
            if (launchIntent != null)
            {
                ctx.StartActivity(launchIntent);//null pointer check in case package name was not found
            }
            else
            {
                try
                {
                    ctx.StartActivity(new Intent(Intent.ActionView, Android.Net.Uri.Parse("market://details?id=" + "com.google.android.apps.books")));
                }
                catch (Exception e)
                {
                    ctx.StartActivity(new Intent(Intent.ActionView, Android.Net.Uri.Parse("https://play.google.com/store/apps/details?id=" + "com.google.android.apps.books")));
                }
            }
        }
    }
}

And then call the openBooks method in the PCL side:

public void OpenBooks(object sender, EventArgs e)
        {
            switch (Device.OS)
            {
                case TargetPlatform.iOS:
                    Device.OpenUri(new Uri("itms-books"));
                    break;
                case TargetPlatform.Android:
                    DependencyService.Get<OpenBookInterface>().openBooks();
                    break;
            }
        }

In my android device I have installed the google play books: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM