9

Im trying to get the URL that was clicked in order to open the app. Im just having trouble finding where I can get that information. I have a manifest which opens app when clicked. The link would be "http://host.com/file.html?param=1&param2=2" which Im trying to give to the app to handle.

 <intent-filter>
           <data android:scheme="http"
                android:host="host.com" 
                android:pathPrefix="/file.html" />
          <action android:name="android.intent.action.VIEW" />
          <category android:name="android.intent.category.BROWSABLE" />
          <category android:name="android.intent.category.DEFAULT" />
 </intent-filter>

EDIT

@Override
  protected void onCreate(Bundle savedInstanceState) 
  {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        Uri uri = intent.getData();
        try {
            url = new URL(uri.getScheme(), uri.getHost(), uri.getPath());
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}
William McCarty
  • 769
  • 3
  • 12
  • 26

3 Answers3

6

You should be able to get the intent data in your activity with the following:

Uri uri = this.getIntent().getData();
URL url = new URL(uri.getScheme(), uri.getHost(), uri.getPath());
Ben Siver
  • 2,758
  • 1
  • 25
  • 42
  • I added my current OnCreate method up top. still isn't seeming to work though. – William McCarty Feb 10 '15 at 00:34
  • My onCreate Method seems to be not being called, from my understanding it will be called when the system calls onCreate and it will redirect to the onCreate I made by overriding it. – William McCarty Feb 10 '15 at 18:44
5

Basically once you obtain your Uri (that is, getIntent().getData()), you can read your complete url as uri.toString(). Assuming your android:host is valid and set, you can also get actual query data by calling uri.getEncodedQuery().

Here is more details from working sample, if needed:

  1. This is my manifest setup:
            <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="boo.acme.com"
                    android:scheme="http" />
            </intent-filter>
  1. This is how I read Uri:
Uri uri = this.getIntent().getData();
  1. Assuming user clicked url "http://boo.acme.com/?ll=43.455095,44.177416", uri.toString() produces "http://boo.acme.com/?ll=43.455095,44.177416" and uri.getEncodedQuery() produces "ll=43.455095,44.177416".

Hope that helps!

ror
  • 3,295
  • 1
  • 19
  • 27
0
Uri uri = this.getIntent().getData();
    try
    {
        URL url = new URL(uri.getScheme(), uri.getHost(), uri.getPath());
    }
    catch (MalformedURLException e)
    {

    }      

You will have to Surround with try/catch.

Dr. Abhishek
  • 158
  • 1
  • 11