简体   繁体   中英

Find URL of sender of intent in a Browsable activity

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:

  • You have a Web page (eg, https://hamidreza.com/somepage )
  • On that Web page, you have a link with a custom scheme (eg, hamidreza://something )
  • On some devices, you have an app with an <activity> with an <intent-filter> that advertises support for that custom scheme
  • 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

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:

  • Opening a Google search result link in Chrome (version 74.0.3729.185)
  • Opening link in Gmail (version 2019.05.12.250526289.release)
    • referrer URL => android-app://com.google.android.gm
  • In Firefox (version 68.2.1) opening the same Google search result link
    • referrer URL => android-app://org.mozilla.firefox

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.

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