Anuran Barman
Anuran Barman

Reputation: 1676

How to know if an activity is opened from a link in android and fetch the GET parameter from the link?

I have both website and android application for the website.when user clicks on a link of my website it opens that up in my android application. But the problem is there is an important information present in the link which i need to fetch and the activity will make a server request upon that. To be precised the link looks like this: www.example.com/post.php?id=123 I need to get the ID part of the link when the activity is opened so that I can show information about posts bearing the ID. I have added the opening of the link in my app with help of intent filter but I am stuck at getting the GET parameter of the link.How to do that?

Upvotes: 0

Views: 29

Answers (2)

SniperDW
SniperDW

Reputation: 303

Once your activity is opened via the link, you can retrieve that URL from the intent like so:

String url = getIntent().getData().toString();

Then you can use .substring to get the parameters:

String id = url.substring(url.indexOf("id=") + 3)

OR, you can use Airbnb's library to achieve the result in a more elegant way, you can learn about it on their GitHub repo.

Upvotes: 1

HB.
HB.

Reputation: 4226

myLink = string.substring(string.lastIndexOf('=') + 1);

If the string/url was www.example.com/post.php?id=123 the string will now return 123.

Upvotes: 1

Related Questions