【问题标题】:How to display Splash screen on OnAppLinkRequestReceived in Xamarin.Forms- Android如何在 Xamarin.Forms-Android 中的 OnAppLinkRequestReceived 上显示启动画面
【发布时间】:2017-07-12 14:27:10
【问题描述】:

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

    }

在我的 mainactivity 开始时,我注册了所有应用链接并拥有如下所示的意图过滤器。

 [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 在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

【问题讨论】:

    标签: android xamarin xamarin.android


    【解决方案1】:

    这里唯一的问题是,单击链接后,用户会看到白屏而不是启动画面。我理解这个问题,因为intentfilters 在mainactivity 上并且从不调用splashactivity。我在启动活动中移动了如下所示的意图过滤器,这次单击链接会显示启动屏幕,但永远不会调用 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);
    }
    

    因此,如果您将 Intent 从您的 SplashActivity 传递到 MainActivity 而没有 ActionData,则永远不会调用 SendOnAppLinkRequestReceived,因此不会调用 OnAppLinkRequestReceived

    要解决此问题,您可以手动将ActionData 添加到SplashActivityOnCreate 中的intent 中:

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

    【讨论】:

    • 非常感谢!你为我节省了几个小时!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 2020-12-14
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多