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

时间:2017-07-12 14:27:11

标签: android xamarin xamarin.android

我已经实现了如下的启动画面 我有一个启动活动作为主启动器启动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);         
        }

    }

在我的主要活动开始时,我会注册所有applinks并拥有如下所示的intentfilters。

 [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());


    }

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

   protected override async void OnAppLinkRequestReceived(Uri uri)
        {
                      // read link and redirect to a page
         }

这里唯一的问题是,点击链接后,用户会看到一个白色的屏幕而不是闪屏。我理解这个问题是因为intentfilters处于主要活动状态,而splashactivity永远不会被调用。我在下面的飞溅活动中移动了如下所示的intentfilters,  这次单击链接会显示启动画面,但从不调用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

1 个答案:

答案 0 :(得分:2)

  

这里唯一的问题是,点击链接后,用户会看到一个白色的屏幕而不是闪屏。我理解这个问题是因为intentfilters处于主要活动状态,而splashactivity永远不会被调用。我在splash事件中移动了类似下面的intentfilters,这次单击链接显示启动画面但是从不调用OnAppLinkRequestReceived函数。有谁知道我怎么能实现这个目标?

调查Xamarin.Forms Source Codes。你可以发现OnAppLinkRequestReceived的调用链是这样的:

FormsAppCompatActivity.LoadApplication ->
FormsAppCompatActivity.CheckForAppLink(Intent) ->
Xamarin.Forms.Application.SendOnAppLinkRequestReceived(Uri) ->
Xamarin.Forms.Application.OnAppLinkRequestReceived(Uri).

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

因此,如果您将意图从SplashActivity传递到MainActivity而没有ActionData,则SendOnAppLinkRequestReceived永远不会被调用,因此{{1不会被调用。

要解决此问题,您可以手动将OnAppLinkRequestReceivedAction添加到Data SplashActivity {/ 1}}中的意图中:

OnCreate