Xamarin Forms: How to open an app from another app?

前端 未结 2 598
生来不讨喜
生来不讨喜 2021-01-21 23:29

I have 2 applications ( A and B ) developed using Xamarin.Forms. I need to open app A from app B.

I have tried like below as per this thread:

In the view

相关标签:
2条回答
  • 2021-01-21 23:40

    Xamarin.Forms.Forms.Context is out of date since XF 2.5 . So you could use Android.App.Application.Context instead of it or use the plugin Plugin.CurrentActivity

    So you could modify the code in Android like following

    using System.Threading.Tasks;
    using Android.App;
    using Android.Content;
    using Android.Content.PM;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using App14;
    using App14.Droid;
    using Xamarin.Forms;
    
    [assembly: Dependency(typeof(OpenAppAndroid))]
    namespace App14.Droid
    {
        public class OpenAppAndroid : IAppHandler
        {
            public Task<bool> LaunchApp(string packageName)
            {
    
    
                bool result = false;
    
                try
                {
    
                    PackageManager pm = Android.App.Application.Context.PackageManager;
    
                    if (IsAppInstalled(packageName))
                    {
                        
                        Intent intent = pm.GetLaunchIntentForPackage(packageName);
                        if (intent != null)
                        {
                          
                            intent.SetFlags(ActivityFlags.NewTask);
                            Android.App.Application.Context.StartActivity(intent);
                        }
                    }
    
                    else
                    {
                      
                        Intent intent = pm.GetLaunchIntentForPackage("the package name of play store on your device");
                        if (intent != null)
                        {
    
                            intent.SetFlags(ActivityFlags.NewTask);
                            Android.App.Application.Context.StartActivity(intent);
                        }
                    }
                }
                catch (ActivityNotFoundException)
                {
                    result = false;
                }
    
                return Task.FromResult(result);
            }
    
    
            private bool IsAppInstalled(string packageName)
            {
                PackageManager pm = Android.App.Application.Context.PackageManager;
                bool installed = false;
                try
                {
                    pm.GetPackageInfo(packageName, PackageInfoFlags.Activities);
                    installed = true;
                }
                catch (PackageManager.NameNotFoundException e)
                {
                    installed = false;
                }
    
                return installed;
            }
    
        }
    }
    

    For iOS you could refer Launch Another IOS App from Xamarin Forms App .

    0 讨论(0)
  • 2021-01-21 23:46

    Have you tried using Xamarin Essentials : Xamarin.Essentials: Launcher

       public class LauncherTest
    {
        public async Task OpenRideShareAsync()
        {
            var supportsUri = await Launcher.CanOpenAsync("lyft://");
            if (supportsUri)
                await Launcher.OpenAsync("lyft://ridetype?id=lyft_line");
        }
    }
    

    Or

    Device.OpenUri(new Uri("fb://page/page_id"));
    
    Device.OpenUri(new Uri("twitter://user?user_id=userid"))
    

    don't forget to use the correct package name when opening specific apps

    0 讨论(0)
提交回复
热议问题