Android 如何侦听自定义 URI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3471503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to listen for a custom URI
提问by Johan
I am working on an application wich has its own URI prefix. (dchub:// in this case)
我正在开发一个应用程序,它有自己的 URI 前缀。(在本例中为 dcub://)
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?
当有人点击dchub://
浏览器中以 开头的链接时,是否可以启动我的应用程序?
So far found a lot of examples the otherway around opening the browser from your app but thats 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 nullpointerexception
s 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?
nullpointerexception
如果我正常启动应用程序,我会得到一些s,如果我从网页打开它就可以正常工作。所以我认为让我们对空值进行一些检查,但这并没有解决它。有什么建议我可以通过选择它来启动应用程序吗?
回答by Thierry-Dimitri Roy
To register a protocol in your android app, add an extra block to the AndroidManifest.xml.
要在您的 android 应用程序中注册协议,请向 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>
回答by DennisK
Don't use data.equals(null). That is bound to fail, you can't call methods on a null object, hence the NPE.
不要使用 data.equals(null)。那肯定会失败,您不能在空对象上调用方法,因此是 NPE。
Why the emtpy code block? In my mind, this is a lot prettier:
为什么是空代码块?在我看来,这更漂亮:
if(data != null){
// code here
}
回答by osama slik
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");
}