问题描述
我已经实现了如下启动画面我有一个启动活动作为主启动器启动 MainActivity
I have implemented splash screen as belowI have a splash activity as main launcher which starts 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);
}
}
并且在我的主要活动开始时,我注册了所有应用链接并具有如下所示的意图过滤器.
and in my onstart of mainactivity, I register all applinks and having intentfilters as shown below.
[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.
So far good, everything works when I click a link, it redirects to my app and hits OnAppLinkRequestReceived in App.Xaml.cs of forms application.
protected override async void OnAppLinkRequestReceived(Uri uri)
{
// read link and redirect to a page
}
这里唯一的问题是用户点击链接后看到的是白屏而不是闪屏.我理解这个问题,因为意图过滤器在主要活动上,而飞溅活动从未被调用.我在飞溅活动中移动了如下所示的意图过滤器,这次单击链接会显示启动画面,但从未调用 OnAppLinkRequestReceived 函数.有谁知道我怎样才能做到这一点?
Only problem here is that after clicking the link user sees a white screen instead of splash screen. I understand the problem because intentfilters are on mainactivity and splashactivity is never called. I moved intentfilters like below on splash activity, this time clicking on a link shows splash screen but OnAppLinkRequestReceived function is never called. Does anybody knows how can I achieve this?
[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
推荐答案
查看 Xamarin.Forms 源代码.可以发现OnAppLinkRequestReceived
的调用链是这样的:
Looking into the Xamarin.Forms Source Codes. You can find out that the calling chain of OnAppLinkRequestReceived
is like this:
FormsAppCompatActivity.LoadApplication ->
FormsAppCompatActivity.CheckForAppLink(Intent) ->
Xamarin.Forms.Application.SendOnAppLinkRequestReceived(Uri) ->
Xamarin.Forms.Application.OnAppLinkRequestReceived(Uri).
并且在 CheckForAppLink(Intent)
中,Xamarin.Forms
执行以下检查:
And in CheckForAppLink(Intent)
, Xamarin.Forms
do the following checks:
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
而没有 Action
和 Data
,SendOnAppLinkRequestReceived
永远不会被调用,因此 OnAppLinkRequestReceived
不会被调用.
So, if you pass the Intent from your SplashActivity
to MainActivity
without Action
and Data
, SendOnAppLinkRequestReceived
will never get called, thus OnAppLinkRequestReceived
won't be called.
为了解决这个问题,你可以在SplashActivity
的OnCreate
中手动添加Action
和Data
到intent中>:
To fix the problem, you can add Action
and Data
manually to the intent in SplashActivity
's OnCreate
:
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);
}
这篇关于如何在 Xamarin.Forms-Android 中的 OnAppLinkRequestReceived 上显示启动画面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!