[英]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:我对此的解释是:
https://hamidreza.com/somepage
)https://hamidreza.com/somepage
: https://hamidreza.com/somepage
)hamidreza://something
)hamidreza://something
)<activity>
with an <intent-filter>
that advertises support for that custom scheme<activity>
和<intent-filter>
的应用程序,用于宣传对该自定义方案的支持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 值示例:
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.