【问题标题】:Xamarin Forms: How to open an app from another app?Xamarin Forms:如何从另一个应用程序打开一个应用程序?
【发布时间】:2020-12-15 01:20:00
【问题描述】:

我有 2 个使用 Xamarin.Forms 开发的应用程序(A 和 B)。我需要从应用 B 打开应用 A。

我已经按照thread进行了如下尝试:

在视图中

var appname = @"otherappId";
var result = await DependencyService.Get<IAppHandler>().LaunchApp(appname);

界面

public interface IAppHandler
{
    Task<bool> LaunchApp(string uri);
}

安卓

[Activity(Label = "OpenAppAndroid")]
public class OpenAppAndroid : Activity, IAppHandler
{
    public Task<bool> LaunchApp(string uri)
    {
        bool result = false;

        try
        {
            var aUri = Android.Net.Uri.Parse(uri.ToString());
            var intent = new Intent(Intent.ActionView, aUri);
            Xamarin.Forms.Forms.Context.StartActivity(intent);
            result = true;
        }
        catch (ActivityNotFoundException)
        {
            result = false;
        }

        return Task.FromResult(result);
    }
}

IOS

public class OpenAppiOS : IAppHandler
{
    public Task<bool> LaunchApp(string uri)
    {
        try
        {
            var canOpen = UIApplication.SharedApplication.CanOpenUrl(new NSUrl(uri));

            if (!canOpen)
                return Task.FromResult(false);

            return Task.FromResult(UIApplication.SharedApplication.OpenUrl(new NSUrl(uri)));

        }
        catch (Exception ex)
        {
            return Task.FromResult(false);
        }
    }
  1. 对于 android,我在运行项目时收到 System.NullReferenceException: 'Object reference not set to an instance of an object.'。我不知道代码中的应用名称是什么?我已经尝试过使用包名。

  2. 另外如果设备上没有安装安卓应用,需要打开Play Store下载应用。

  3. 我没有测试 iOS 部分,因为 Mac 不可用。上面的代码适用于 iOS 吗?另外如果iPhone上没有安装应用,需要打开AppStore下载应用。

  4. 另外,我喜欢为 UWP 实现相同的功能。

参考: https://forums.xamarin.com/discussion/92666/detect-and-open-another-app-on-device Is it possible to open another app in my app with xamarin.form?

【问题讨论】:

    标签: android ios xamarin.forms uwp


    【解决方案1】:

    Xamarin.Forms.Forms.Context 自 XF 2.5 以来已过期。所以你可以使用Android.App.Application.Context 代替它或使用插件Plugin.CurrentActivity

    所以你可以像下面这样修改Android中的代码

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

    对于 iOS,您可以参考 Launch Another IOS App from Xamarin Forms App

    【讨论】:

    • 这是您设备上 google play store(或其他应用商店)的包名。你需要检查一下。
    • Device.OpenUri(new Uri("xxx"))
    • 我在 ios 应用程序中遇到了相同功能的问题。你能看看吗? *.com/questions/63751993/…
    【解决方案2】:

    您是否尝试过使用 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");
        }
    }
    

    或者

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

    在打开特定应用时不要忘记使用正确的包名

    【讨论】:

    • 仅供参考,此答案适用于 URI 端点,与启动另一个 apk 不同
    • @DukeDidntNukeEm 这是 APK 的,如果你想打开一个浏览器,你可以使用下面的代码 await Browser.OpenAsync(uri, BrowserLaunchMode.SystemPreferred);
    • 谢谢!也许我很困惑,我认为一个 URI 调用了一个 apk,但是要在代码中直接调用一个 apk,你必须以不同于这里的答案的方式来做 - 这有意义吗?比如说,你想打开谷歌地图,但不是从可点击的位置或其他东西开始,只是加载谷歌地图 apk :)
    最近更新 更多