I handle a specific URL scheme in one of my activities intent-filter. 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. 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.
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. How can I get A.com
address in the intent in my activity? 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. I want to get example.com from the intent.
I want to get the URL which user came from it to my app from browser
My interpretation of this is:
https://hamidreza.com/somepage
)hamidreza://something
)<activity>
with an <intent-filter>
that advertises support for that custom schemehttps://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. 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.
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
). Then you can obtain that from query parameters on the Uri
delivered to your activity.
Calling getReferrer()
returns the referrer URI containing the package name that started the Intent. There is no guarantee that the referrer will always be known!
Examples of URL values provided by different referring apps in November 2019:
Important caveat from 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. Otherwise, it always returns the referrer of the original Intent.
For more info see 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.
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.
In 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
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.getData() != null) {
Uri uri = intent.getData();
List<String> segments = uri.getPathSegments();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.