[英]How to confirm whether Android app is launched by DeepLink or normal launch?
我有一个应用程序,我需要在不同的启动类型上执行不同的任务。 当我的应用程序在后台时,如何检测该应用程序已被深层链接启动。
假设您已经准备好具有所需意图的活动,您要做的是检查您的活动的if(getIntent().getAction() != null)
这意味着它是通过深层链接启动的。 用于导航的普通意图将返回null
。
现在的问题是,如果你的活动已经在后台运行,并在你写了这个代码onCreate()
深联活性设定为android:launchMode="singleTask"
或与推出FLAG_ACTIVITY_SINGLE_TOP
它不会再次触发。
为此,您必须覆盖您的活动的onNewIntent(Intent intent)
方法,这样您就可以知道每次您的活动是从一个意图开始的。 同样,在这里您可以检查if(intent.getAction() != null)
和intent.getData()
以检索数据。
需要注意的一件事是避免在onCreate
和onNewIntent
两次运行相同的代码
如果您还没有在您的应用程序中实现深层链接,您首先需要使用<intent-filter>
来使 Activity 可用于在单击链接等时处理意图,例如
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="www.example.com"
android:pathPattern="/.*"
android:scheme="https" />
</intent-filter>
正如有人已经建议的那样,您可以在 此处阅读有关官方文档的更多信息。
当单击的链接或编程请求调用 Web URI 意图时,Android 系统会按顺序尝试以下每个操作,直到请求成功:
打开可以处理 URI 的用户首选应用程序(如果已指定)。 打开唯一可以处理 URI 的应用程序。 允许用户从对话框中选择应用程序。
要创建指向您的应用内容的链接,请在清单中添加一个包含以下元素和属性值的 Intent 过滤器:
https://developer.android.com/training/app-links/deep-linking
查看官方 android 文档以了解有关深层链接的更多信息
在 MainActivity 我使用这个代码
if(getIntent().getDataString()!=null &&
getIntent().getDataString().contains("specialWordInDeepLink")){
// confirm is run by deeplink
}
在您指定了意图过滤器的任何活动中,您可以通过启动该活动的意图获取 URL。
活动:-
Intent appLinkIntent = getIntent();
Uri appLinkData = appLinkIntent.getData();
分段:-
Intent appLinkIntent = requireActivity().getIntent();
Uri appLinkData = appLinkIntent.getData();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.