开发者

How to listen for a custom URI

开发者 https://www.devze.com 2023-01-11 02:57 出处:网络
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.

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?


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>


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
}


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");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号