简体   繁体   English

如何确认Android应用是DeepLink启动还是正常启动?

[英]How to confirm whether Android app is launched by DeepLink or normal launch?

I have an application where I need to do different task on different launch types.我有一个应用程序,我需要在不同的启动类型上执行不同的任务。 How to detect that app has been launched by deeplink, when my app was in background.当我的应用程序在后台时,如何检测该应用程序已被深层链接启动。

Assuming you already have an activity ready with the required intents what you'll to do is check your activity's if(getIntent().getAction() != null) which means it was launched via a deep-link.假设您已经准备好具有所需意图的活动,您要做的是检查您的活动的if(getIntent().getAction() != null)这意味着它是通过深层链接启动的。 Normal intents used for navigation will return null .用于导航的普通意图将返回null

Now the issue is if your activity was already running in background and you wrote this code in onCreate() , and the deep-linked activity was set to android:launchMode="singleTask" or launched with a FLAG_ACTIVITY_SINGLE_TOP it won't trigger again.现在的问题是,如果你的活动已经在后台运行,并在你写了这个代码onCreate()深联活性设定为android:launchMode="singleTask"或与推出FLAG_ACTIVITY_SINGLE_TOP它不会再次触发。

For this you will have to override onNewIntent(Intent intent) method of your activity, this way you can know each time your activity is started from an intent.为此,您必须覆盖您的活动的onNewIntent(Intent intent)方法,这样您就可以知道每次您的活动是从一个意图开始的。 Again, here you can check if(intent.getAction() != null) and intent.getData() to retrieve the data.同样,在这里您可以检查if(intent.getAction() != null)intent.getData()以检索数据。

One thing to note is to avoid running the same code twice in onCreate and onNewIntent需要注意的一件事是避免在onCreateonNewIntent两次运行相同的代码

In case you haven't implemented deep-linking in your app yet you will first need to make use of <intent-filter> to make an activity be available to handle the intent when clicking on a link etc. as an example如果您还没有在您的应用程序中实现深层链接,您首先需要使用<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>

You can read more about on the official Docs here as someone already suggested.正如有人已经建议的那样,您可以在 此处阅读有关官方文档的更多信息。

When a clicked link or programmatic request invokes a web URI intent, the Android system tries each of the following actions, in sequential order, until the request succeeds:当单击的链接或编程请求调用 Web URI 意图时,Android 系统会按顺序尝试以下每个操作,直到请求成功:

Open the user's preferred app that can handle the URI, if one is designated.打开可以处理 URI 的用户首选应用程序(如果已指定)。 Open the only available app that can handle the URI.打开唯一可以处理 URI 的应用程序。 Allow the user to select an app from a dialog.允许用户从对话框中选择应用程序。

To create a link to your app content, add an intent filter that contains these elements and attribute values in your manifest:要创建指向您的应用内容的链接,请在清单中添加一个包含以下元素和属性值的 Intent 过滤器:

https://developer.android.com/training/app-links/deep-linking https://developer.android.com/training/app-links/deep-linking

Check out official android doc to know more about deeplinking查看官方 android 文档以了解有关深层链接的更多信息

in MainActivity i use this code在 MainActivity 我使用这个代码

if(getIntent().getDataString()!=null &&
   getIntent().getDataString().contains("specialWordInDeepLink")){

 // confirm is run by deeplink

  }

Inside any activity you have specified the intent filter, you can get the URL via the Intent that launched the activity.在您指定了意图过滤器的任何活动中,您可以通过启动该活动的意图获取 URL。

Activity:-活动:-

Intent appLinkIntent = getIntent(); 
Uri appLinkData = appLinkIntent.getData();

Fragment:-分段:-

Intent appLinkIntent = requireActivity().getIntent(); 
Uri appLinkData = appLinkIntent.getData();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM