简体   繁体   English

在可浏览活动中查找意图发件人的 URL

[英]Find URL of sender of intent in a Browsable activity

I handle a specific URL scheme in one of my activities intent-filter.我在我的一个活动意图过滤器中处理特定的 URL 方案。 My users can put their URL in any webpages on the Internet and other users which are browsing them in browser can click on them and my app start to work.我的用户可以将他们的 URL 放在互联网上的任何网页中,其他在浏览器中浏览它们的用户可以点击它们,我的应用程序开始工作。 This work perfectly.这工作完美。

Now my problem is that I want to find out the URL which this user has came from that, in my activity.现在我的问题是我想在我的活动中找出该用户来自的 URL。

For example a user put his link in a website called A.com and another user which is browsing A.com click on this link, so it come to my app.例如,一个用户将他的链接放在一个名为A.com的网站上,另一个正在浏览A.com用户点击这个链接,所以它来到了我的应用程序。 How can I get A.com address in the intent in my activity?如何在我的活动的意图中获取A.com地址? Is it possible?是否可以?

Scenario for more information:有关更多信息的场景:
For example I handle this kind of link in one my activity intent-filters data: hisapp://host/path A user put this in web for example a web site: example.com, and another user click on it and come to my app.例如,我在一个我的活动意图过滤数据中处理这种链接: hisapp://host/path 一个用户把它放在网络中,例如一个网站:example.com,另一个用户点击它并来到我的应用程序。 I want to get example.com from the intent.我想从意图中获取 example.com。

I want to get the URL which user came from it to my app from browser我想从浏览器获取用户从它到我的应用程序的 URL

My interpretation of this is:我对此的解释是:

  • You have a Web page (eg, https://hamidreza.com/somepage )您有一个网页(例如, https://hamidreza.com/somepage : https://hamidreza.com/somepage
  • On that Web page, you have a link with a custom scheme (eg, hamidreza://something )在该网页上,您有一个自定义方案的链接(例如, hamidreza://something
  • On some devices, you have an app with an <activity> with an <intent-filter> that advertises support for that custom scheme在某些设备上,您有一个带有<activity><intent-filter>的应用程序,用于宣传对该自定义方案的支持
  • When the user visits that Web page from a browser on their device, and they click that link, which starts your activity... you want to be able to find out that this all started with https://hamidreza.com/somepage当用户从他们设备上的浏览器访问该网页时,他们单击该链接,这将启动您的活动……您希望能够发现这一切都始于https://hamidreza.com/somepage

You are welcome to examine the extras on the Intent that is delivered to your activity (via getIntent() ) and see if the referer URL is in there somewhere.欢迎您检查传递给您的活动的Intent的额外内容(通过getIntent() ),并查看引用 URL 是否在某处。 Perhaps for a few browsers, it is.也许对于一些浏览器来说,确实如此。 I expect that for most browsers, it is not.我希望对于大多数浏览器来说,事实并非如此。 And for those, there is no way for you to know the URL that contained the URL that started your activity.对于那些,您无法知道包含启动您的活动的 URL 的 URL。

As Selvin pointed out in a comment, you are welcome to embed that sort of information in your custom URL (eg, hamidreza://something?referer=https://hamidreza.com/somepage ).正如 Selvin 在评论中指出的那样,欢迎您在自定义 URL 中嵌入此类信息(例如, hamidreza://something?referer=https://hamidreza.com/somepage )。 Then you can obtain that from query parameters on the Uri delivered to your activity.然后,您可以从传递给您的活动的Uri上的查询参数中获取该信息。

Calling getReferrer() returns the referrer URI containing the package name that started the Intent.调用getReferrer()返回包含启动 Intent 的包名称的引用 URI。 There is no guarantee that the referrer will always be known!不能保证推荐人永远是已知的!

Examples of URL values provided by different referring apps in November 2019: 2019 年 11 月不同引荐应用提供的 URL 值示例:

  • Opening a Google search result link in Chrome (version 74.0.3729.185)在 Chrome(版本 74.0.3729.185)中打开 Google 搜索结果链接
  • Opening link in Gmail (version 2019.05.12.250526289.release)在 Gmail 中打开链接(版本 2019.05.12.250526289.release)
    • referrer URL => android-app://com.google.android.gm引荐网址 => android-app://com.google.android.gm
  • In Firefox (version 68.2.1) opening the same Google search result link在 Firefox(版本 68.2.1)中打开相同的 Google 搜索结果链接
    • referrer URL => android-app://org.mozilla.firefox引用 URL => android-app://org.mozilla.firefox

Important caveat from developer.android.com: developer.android.com 的重要警告:

If called while inside the handling of onNewIntent(Intent), getReferrer() will return the referrer that submitted that new intent to the activity.如果在处理 onNewIntent(Intent) 时调用,getReferrer() 将返回将新意图提交给活动的引用者。 Otherwise, it always returns the referrer of the original Intent.否则,它总是返回原始 Intent 的引用者。

For more info see https://developer.android.com/reference/android/app/Activity.html#getReferrer()有关更多信息,请参阅https://developer.android.com/reference/android/app/Activity.html#getReferrer()

Now my problem is that I want to find out the URL which this user has came from that, in my activity. 现在我的问题是,我想在我的活动中找出此用户来自的URL。

The Url will passed as data in intent to your activity . 网址将作为数据传递给您的活动。 Use getQueryParameter to get parameters from Uri or you can get it from segments. 使用getQueryParameter可以从Uri获取参数,也可以从细分中获取参数。

In onCreate 在onCreate中

 if (getIntent().getData() != null) {
        Uri uri = getIntent().getData();// this is the url
        List<String> segments = uri.getPathSegments();// this is the url segments 
    }

In onNewIntent 在onNewIntent

 @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (intent.getData() != null) {
        Uri uri = intent.getData();
        List<String> segments = uri.getPathSegments();
    }
}

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

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