12

I am working on an application which has its own URI prefix. (dchub:// in this case)

Searching all over and read a lot but I got a bit confused.

Is it possible to start my application when someone clicks on a link starting with dchub:// in the browser?

So far found a lot of examples the other way around opening the browser from your app but that's not what I'm looking for.

Update

Thanks a lot, I've figured that, now I'm a bit stuck in the next part.

Uri data = getIntent().getData(); 
if (data.equals(null)) { } else { 
    String scheme = data.getScheme(); 
    String host = data.getHost(); 
    int port = data.getPort(); 
}

I got some nullpointerexceptions if I start the app normally, it works fine if I open from the webpage. So I thought lets include some check for nullvalue but that didn't solve it. any suggestions how I can start the app just by selecting it?

Jason Aller
  • 3,391
  • 28
  • 37
  • 36
Johan
  • 133
  • 1
  • 1
  • 6
  • http://stackoverflow.com/questions/3469908/make-a-link-in-the-android-browser-start-up-my-app just found this reply, gonna try if this works. – Johan Aug 12 '10 at 19:58
  • Good question, but please ask additional questions as separate questions, not as an update to your original question... – poplitea Dec 05 '13 at 00:35

3 Answers3

26

To register a protocol in your android app, add an extra block to the AndroidManifest.xml.

<manifest>
 <application>
   <activity>
           <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:scheme="dchub"/>
            </intent-filter>
   </activity>
 </application>
</manifest>
Thierry-Dimitri Roy
  • 8,213
  • 10
  • 57
  • 81
  • Thanks a lot, i've figured that, now i'm a bit stuck in the next part. [code] Uri data = getIntent().getData(); if (data.equals(null)) { } else { String scheme = data.getScheme(); String host = data.getHost(); int port = data.getPort(); } [/code] i got some nullpointerexceptions if i start the app normally, it works fine if i open from the webpage. So i thought lets include some check for nullvalue but that didn't solve it. any suggestions how i can start the app just by selecting it? //sorry for bad formatting, the manual doesn't help it says four spaces, did that resaved no changes :S – Johan Aug 12 '10 at 21:10
  • 3
    if data is null "data.equals(null)" will cause a nullpointer exception... check if(data == null) instead – jpm Apr 01 '11 at 19:20
10

Don't use data.equals(null). That is bound to fail, you can't call methods on a null object, hence the NPE.

Why the emtpy code block? In my mind, this is a lot prettier:

if(data != null){
    // code here
}
DennisK
  • 431
  • 4
  • 8
-1

Try this code:

try {
    Uri data = getIntent().getData();
    if (data.equals(null)) { 
    } else { 
        String scheme = data.getScheme();
        String host = data.getHost();
        int port = data.getPort(); 
        //type what u want
        tv.setText("any thing");
     }      
} catch (NullPointerException e) {
      // TODO: handle exception
  tv.setText("Null");
}
user
  • 85,380
  • 17
  • 189
  • 186