如何在 Xamarin.Forms-Android 中的 OnAppLinkRequestReceived 上显示启动屏幕

bat*_*aci 1 android xamarin.android xamarin

我已经实现了启动屏幕,如下所示我有一个启动活动作为主启动器,它启动 MainActivity

[Activity(Theme = "@style/MyTheme.Splash",  
              MainLauncher = true,  
              Immersive = true,
              ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait,
              NoHistory = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] 
    public class SplashActivity : AppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            var intent = new Intent(this, typeof(MainActivity));
            StartActivity(intent);         
        }

    }
Run Code Online (Sandbox Code Playgroud)

在我的主要活动开始时,我注册了所有应用程序链接并具有意图过滤器,如下所示。

 [Activity(Label = "myapp",
        Icon = "@drawable/icon",
        Theme = "@style/MyTheme",
         MainLauncher = false,
        ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]  
      [IntentFilter(new[] { Intent.ActionView },
      Categories = new[] {
            Intent.ActionView,
            Intent.CategoryDefault,
            Intent.CategoryBrowsable
      },
      DataScheme = "http",
      DataHost = "myapp.azurewebsites.net",
      DataPathPrefix = "/app/", AutoVerify = true)
  ]
    [IntentFilter(new[] { Intent.ActionView },
      Categories = new[] {
            Intent.ActionView,
            Intent.CategoryDefault,
            Intent.CategoryBrowsable
      },
      DataScheme = "https",
      DataHost = "myapp.azurewebsites.net",
      DataPathPrefix = "/app/", AutoVerify = true)
  ]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
     protected override void OnCreate(Bundle bundle)
        {

            // TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);


            global::Xamarin.Forms.Forms.Init(this, bundle);
            AndroidAppLinks.Init(this);       

            LoadApplication(new App());


    }
Run Code Online (Sandbox Code Playgroud)

到目前为止,一切正常,当我单击链接时,它会重定向到我的应用程序并点击表单应用程序的 App.Xaml.cs 中的 OnAppLinkRequestReceived 。

   protected override async void OnAppLinkRequestReceived(Uri uri)
        {
                      // read link and redirect to a page
         }
Run Code Online (Sandbox Code Playgroud)

这里唯一的问题是,单击链接后,用户看到的是白屏而不是闪屏。我理解这个问题,因为意图过滤器位于 mainactivity 上,而 splashactivity 从未被调用。我在启动活动中移动了意图过滤器,如下所示,这次单击链接会显示启动屏幕,但从未调用 OnAppLinkRequestReceived 函数。有谁知道我怎样才能实现这一目标?

    [Activity(Theme = "@style/MyTheme.Splash", 
              MainLauncher = true,  
              Immersive = true,
              ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait,
              NoHistory = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]  
    [IntentFilter(new[] { Intent.ActionView },
      Categories = new[] {
            Intent.ActionView,
            Intent.CategoryDefault,
            Intent.CategoryBrowsable
      },
      DataScheme = "http",
      DataHost = "myapp.azurewebsites.net",
      DataPathPrefix = "/app/", AutoVerify = true)
  ]
    [IntentFilter(new[] { Intent.ActionView },
      Categories = new[] {
            Intent.ActionView,
            Intent.CategoryDefault,
            Intent.CategoryBrowsable
      },
      DataScheme = "https",
      DataHost = "myapp.azurewebsites.net",
      DataPathPrefix = "/app/", AutoVerify = true)
  ]
    public class SplashActivity : AppCompatActivity
Run Code Online (Sandbox Code Playgroud)

Elv*_*SFT 5

这里唯一的问题是,单击链接后,用户看到的是白屏而不是闪屏。我理解这个问题,因为意图过滤器位于 mainactivity 上,而 splashactivity 从未被调用。我在启动活动中移动了意图过滤器,如下所示,这次单击链接会显示启动屏幕,但从未调用 OnAppLinkRequestReceived 函数。有谁知道我怎样才能实现这一目标?

研究Xamarin.Forms 源代码。可以发现 的调用链OnAppLinkRequestReceived是这样的:

FormsAppCompatActivity.LoadApplication ->
FormsAppCompatActivity.CheckForAppLink(Intent) ->
Xamarin.Forms.Application.SendOnAppLinkRequestReceived(Uri) ->
Xamarin.Forms.Application.OnAppLinkRequestReceived(Uri).
Run Code Online (Sandbox Code Playgroud)

在 中CheckForAppLink(Intent)Xamarin.Forms执行以下检查:

void CheckForAppLink(Intent intent)
{
    string action = intent.Action;
    string strLink = intent.DataString;
    if (Intent.ActionView != action || string.IsNullOrWhiteSpace(strLink))
            return;

    var link = new Uri(strLink);
    _application?.SendOnAppLinkRequestReceived(link);
}
Run Code Online (Sandbox Code Playgroud)

因此,如果您将 Intent 从 your 传递SplashActivityMainActivitywithoutActionDataSendOnAppLinkRequestReceived则永远不会被调用,因此OnAppLinkRequestReceived不会被调用。

要解决此问题,您可以将Action和手动添加到'sData中的意图:SplashActivityOnCreate

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    string action = Intent.Action;
    string strLink = Intent.DataString;
    Intent intent = new Intent(Application.Context, typeof(MainActivity));
    if (Android.Content.Intent.ActionView == action && !string.IsNullOrWhiteSpace(strLink))
    {
        intent.SetAction(Intent.ActionView);
        intent.SetData(Intent.Data);
    }
    StartActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)