如何在 Android 上实现我自己的 URI 方案
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2448213/
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 implement my very own URI scheme on Android
提问by punnie
Say I want to define that an URI such as:
假设我想定义一个 URI,例如:
myapp://path/to/what/i/want?d=This%20is%20a%20test
must be handled by my own application, or service. Notice that the scheme is "myapp"
and not "http"
, or "ftp"
. That is precisely what I intend: to define my own URI schema globally for the Android OS. Is this possible?
必须由我自己的应用程序或服务处理。请注意,该方案是"myapp"
and not "http"
, or "ftp"
。这正是我的意图:为 Android 操作系统全局定义我自己的 URI 模式。这可能吗?
This is somewhat analogous to what some programs already do on, e.g., Windows systems, such as Skype (skype://
) or any torrent downloader program (torrent://
).
这有点类似于某些程序已经在例如 Windows 系统上执行的操作,例如 Skype ( skype://
) 或任何 torrent 下载程序 ( torrent://
)。
回答by Dan Lew
This is very possible; you define the URI scheme in your AndroidManifest.xml, using the <data>
element. You setup an intent filter with the <data>
element filled out, and you'll be able to create your own scheme. (More on intent filters and intent resolution here.)
这是很有可能的;您使用<data>
元素在 AndroidManifest.xml 中定义 URI 方案。您设置了一个<data>
填充了元素的意图过滤器,您将能够创建自己的方案。(更多关于意图过滤器和意图解析在这里。)
Here's a short example:
这是一个简短的例子:
<activity android:name=".MyUriActivity">
<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="myapp" android:host="path" />
</intent-filter>
</activity>
As per how implicit intents work, you need to define at least one action and one category as well; here I picked VIEW as the action (though it could be anything), and made sure to add the DEFAULT category (as this is required for all implicit intents). Also notice how I added the category BROWSABLE - this is not necessary, but it will allow your URIs to be openable from the browser (a nifty feature).
根据隐式意图的工作原理,您还需要至少定义一个动作和一个类别;在这里,我选择 VIEW 作为动作(尽管它可以是任何东西),并确保添加 DEFAULT 类别(因为这是所有隐式意图所必需的)。还要注意我是如何添加 BROWSABLE 类别的——这不是必需的,但它可以让你的 URI 从浏览器中打开(一个漂亮的功能)。
回答by Diego Palomar
Complementing the @DanielLewanswer, to get the values of the parameteres you have to do this:
补充@DanielLew答案,要获取参数的值,您必须这样做:
URI example: myapp://path/to/what/i/want?keyOne=valueOne&keyTwo=valueTwo
URI 示例: myapp://path/to/what/i/want?keyOne=valueOne&keyTwo=valueTwo
in your activity:
在您的活动中:
Intent intent = getIntent();
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
Uri uri = intent.getData();
String valueOne = uri.getQueryParameter("keyOne");
String valueTwo = uri.getQueryParameter("keyTwo");
}
回答by hackbod
I strongly recommend that you not define your own scheme. This goes against the web standards for URI schemes, which attempts to rigidly control those names for good reason -- to avoid name conflicts between different entities. Once you put a link to your scheme on a web site, you have put that little name into entire the entire Internet's namespace, and should be following those standards.
我强烈建议您不要定义自己的方案。这违背了 URI 方案的 Web 标准,该标准试图严格控制这些名称,这是有充分理由的——以避免不同实体之间的名称冲突。一旦您在网站上放置了指向您的方案的链接,您就将这个小名称放入了整个 Internet 的命名空间中,并且应该遵循这些标准。
If you just want to be able to have a link to your own app, I recommend you follow the approach I described here:
如果您只想拥有指向您自己的应用程序的链接,我建议您遵循我在此处描述的方法:
如何通过在 Android 操作系统的浏览器中调用 URL 来注册一些 URL 命名空间 (myapp://app.start/) 以访问您的程序?
回答by Christian
Another alternate approach to Diego's is to use a library:
Diego 的另一种替代方法是使用库:
https://github.com/airbnb/DeepLinkDispatch
https://github.com/airbnb/DeepLinkDispatch
You can easily declare the URIs you'd like to handle and the parameters you'd like to extract through annotations on the Activity, like:
您可以通过 Activity 上的注释轻松声明要处理的 URI 和要提取的参数,例如:
@DeepLink("path/to/what/i/want")
public class SomeActivity extends Activity {
...
}
As a plus, the query parameters will also be passed along to the Activity as well.
另外,查询参数也将传递给活动。
回答by Weidian Huang
As the question is asked years ago, and Android is evolved a lot on this URI scheme.
From original URI scheme, to deep link, and now Android App Links.
正如多年前提出的问题,Android 在这个 URI 方案上有很大的发展。
从最初的 URI 方案,到深层链接,再到现在的 Android 应用链接。
Android now recommends to use HTTP URLs, not define your own URI scheme. Because Android App Links use HTTP URLs that link to a website domain you own, so no other app can use your links. You can check the comparison of deep link and Android App links from here
Android 现在建议使用 HTTP URL,而不是定义您自己的 URI 方案。由于 Android 应用链接使用链接到您拥有的网站域的 HTTP URL,因此其他应用无法使用您的链接。您可以从这里查看深层链接和 Android 应用程序链接的比较
Now you can easily add a URI scheme by using Android Studio option: Tools > App Links Assistant. Please refer the detail to Android document: https://developer.android.com/studio/write/app-link-indexing.html
现在,您可以使用 Android Studio 选项轻松添加 URI 方案:工具 > 应用链接助手。详情请参考Android文档:https: //developer.android.com/studio/write/app-link-indexing.html