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

2023-09-11 10:44:50 作者:情定三生。

说我要定义的URI,例如:

Say I want to define that an URI such as:

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

必须由我自己的应用程序或服务来处理。请注意,该方案MyApp的,而不是HTTPFTP 。这是precisely我打算:在全球范围内定义自己的URI模式的Andr​​oid操作系统。这可能吗?

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?

这有点类似于一些什么项目已经在做的,例如,Windows系统中,如Skype( SKYPE:// )或任何BT下载软件程序(洪流://

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

推荐答案

这是非常有可能的;您可以定义你的Andr​​oidManifest.xml中的URI方案,使用<data>元件。您设置的意图过滤器与&lt;数据&GT; 元素填写,你就可以创建自己的方案。 (更多关于意图过滤器和意图分辨率这里。 )

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>

根据如何隐含意图的工作,你需要定义至少一个动作和一类为好;在这里我挑观为行动(虽然它可以是任何东西),并确保添加默认类别(因为这是必需的所有隐含意图)。还要注意我是如何添加的类别可浏览的 - 这是没有必要的,但它可以让你的URI是从浏览器(一个漂亮的功能)可开启

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