如何在 Android 上实现我自己的 URI 方案

发布于 2024-08-25 00:52:46 字数 361 浏览 3 评论 0原文

假设我想定义一个 URI,例如:

myapp://path/to/what/i/want?d=This%20is%20a%20test

必须由我自己的应用程序或服务处理。请注意,该方案是 "myapp",而不是 "http""ftp"。这正是我的意图:为 Android 操作系统全局定义我自己的 URI 模式。这可能吗?

这有点类似于某些程序已经在 Windows 系统上执行的操作,例如 Skype (skype://) 或任何 torrent 下载程序 (torrent:// )。

Say I want to define that an URI such as:

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?

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://).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

瞳孔里扚悲伤 2024-09-01 00:52:46

这是很有可能的;您可以使用 < 在 AndroidManifest.xml 中定义 URI 方案;data> 元素。您可以设置一个填写了 元素的 Intent 过滤器,然后您就可以创建自己的方案。 (更多有关意图过滤器和意图解析的信息。)

这是简短的示例:

<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>

根据隐式意图的工作原理,您还需要定义至少一个操作和一个类别;在这里,我选择 VIEW 作为操作(尽管它可以是任何内容),并确保添加 DEFAULT 类别(因为这是所有隐式意图所必需的)。另请注意我如何添加 BROWSABLE 类别 - 这不是必需的,但它将允许您的 URI 可从浏览器打开(一个很棒的功能)。

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.)

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).

望喜 2024-09-01 00:52:46

补充 @DanielLew答案,要获取参数的值,您必须执行以下操作:

URI 示例:myapp://path/to/what/i/want?keyOne=valueOne&keyTwo=valueTwo

在您的活动中:

Intent intent = getIntent();
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
  Uri uri = intent.getData();
  String valueOne = uri.getQueryParameter("keyOne");
  String valueTwo = uri.getQueryParameter("keyTwo");
}

Complementing the @DanielLew answer, to get the values of the parameteres you have to do this:

URI example: 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");
}
不离久伴 2024-09-01 00:52:46

我强烈建议您不要定义自己的方案。这违背了 URI 方案的 Web 标准,该标准试图严格控制这些名称,这是为了避免不同实体之间的名称冲突。一旦您在网站上放置了指向您的方案的链接,您就已经将这个小名称放入了整个互联网的命名空间中,并且应该遵循这些标准。

如果您只想能够拥有指向自己的应用程序的链接,我建议您遵循我在此处描述的方法:

如何注册一些 URL 命名空间 (myapp://app.start/) 以通过在浏览器中调用 URL 来访问您的程序在 Android 操作系统中?

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.

If you just want to be able to have a link to your own app, I recommend you follow the approach I described here:

How to register some URL namespace (myapp://app.start/) for accessing your program by calling a URL in browser in Android OS?

万劫不复 2024-09-01 00:52:46

正如几年前提出的问题一样,Android 在这个 URI 方案上已经发展了很多。
从最初的URI方案,到深层链接,再到现在的Android应用程序链接。

Android 现在建议使用 HTTP URL,而不是定义您自己的 URI 方案。由于 Android 应用程序链接使用链接到您拥有的网站域的 HTTP URL,因此其他应用程序无法使用您的链接。您可以从此处查看深层链接和 Android 应用链接的比较

现在您可以轻松添加使用 Android Studio 选项创建 URI 方案:Tools >应用程序链接助手。
详情请参考 Android 文档:https://developer.android.com /studio/write/app-link-indexing.html

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 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

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

撧情箌佬 2024-09-01 00:52:46

Diego 的另一种替代方法是使用库:

https://github.com/airbnb/DeepLinkDispatch

可以轻松地声明您想要处理的 URI 以及您想要通过 Activity 上的注释提取的参数,例如:

@DeepLink("path/to/what/i/want")
public class SomeActivity extends Activity {
  ...
}

作为一个优点,查询参数也将传递给 Activity。

Another alternate approach to Diego's is to use a library:

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:

@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文