如何实现我自己在Android URI方案 [英] How to implement my very own URI scheme on Android
问题描述
说我要定义的URI,例如:
Say I want to define that an URI such as:
myapp://path/to/what/i/want?d=This%20is%20a%20test
必须由我自己的应用程序或服务来处理。请注意,该方案MyApp的
,而不是HTTP
或FTP
。这是precisely我打算:在全球范围内定义自己的URI模式的Android操作系统。这可能吗?
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://
).
推荐答案
这是非常有可能的;您可以定义你的AndroidManifest.xml中的URI方案,使用<一个href="http://developer.android.com/intl/zh-TW/guide/topics/manifest/data-element.html"><$c$c><data>$c$c>元件。您设置的意图过滤器与&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).
这篇关于如何实现我自己在Android URI方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!