Anton Sobolev
Anton Sobolev

Reputation: 135

Android get url from browser

I am try to open my app on url click and get value from it, url looks like : path.com/item?key=value

in manifest:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />

    <data
        android:host="path.com"
        android:pathPrefix="/item"
        android:scheme="http" />
</intent-filter>

but

Uri data = getIntent().getData(); 
data.getPathSegments();
Log.d("TAG", "param is:" + params.get(0);

returns: param is:item /item . how to get key=value or full url

Upvotes: 4

Views: 4619

Answers (3)

Nacho
Nacho

Reputation: 386

To get the full Uri string, you can do toString() on the Uri. But I would advise against that. Most likely, what you want is already in a method of Uri.

If you want the full path, use getPath(). If you want the full Uri as a String, toString(). If you want a specific query parameter, getQueryParameter(String key). If you want the full query string, getQuery(). If you want the keys you got in the query parameter, getQueryParameterNames().

There's a lot more. Those are the ones that I think might be useful to you based on your question.

You can, of course, do your own parsing, but most of the time it's unnecessary and error prone.

See http://developer.android.com/reference/android/net/Uri.html

Upvotes: 1

Abhinav Tyagi
Abhinav Tyagi

Reputation: 5266

This is how I did recently in my app

Use following intent filter

<intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="<SCHEME_NAME>" />

            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

In your URL use following

for Chrome

window.location = "intent://item?key=value#Intent;scheme=<SCHEME_NAME>;package=<APP_PACKAGE>;";`

other bowsers

window.location = "<SCHEME_NAME>://item?key=value";

In the Activity class use following

String uriPath = getIntent().getDataString();

uriPath will have string "item?key=value" which you can parse based on patterns.

You can Modify the "item?key=value" in your JS to send only value if required. Do experiment on this.

Follow the link for more https://developer.android.com/training/app-indexing/deep-linking.html

Upvotes: 5

Kartheek
Kartheek

Reputation: 7214

If the intent was to visit a URL (i.e., intent.getAction() == Intent.ACTION_VIEW), then intent.getData() will return that URL.

Uri data = intent.getData();
String path = data.getPath();

Upvotes: 1

Related Questions