自定协议

首先看几个例子,更能说明本文要表达的意思,如果对例子不感兴趣,可以直接看下一阶段的分析。
PC端的例子:
在浏览器地址栏输入tencent://message/?uin=88888888,然后回车,发现可以和QQ的陌生人聊天了,这个功能很早就有了。
Android的例子:
做Android开发的朋友(当然IOS也有类似的功能)都知道可以通过意图调用系统的功能,比如拨打电话:
Intent intent = new Intent(); 
intent.setAction(Intent.ACTION_CALL);  
intent.setData(Uri.parse("tel:15899996666")); 
startActivity(intent);
IOS的例子:
苹果的企业账户可以通过一种协议直接从公司的网站下载app并安装,不需要通过AppStore来下载,比如在html页面添加如下代码:<a href="itms-services://?action=download-manifest&amp;url=http://192.168.0.100:8080/latest/testapp.plist(plist文件的地址)">Install 应用</a>,然后通过浏览器打开,点击,发现可以直接下载应用了。


看完三个例子细心的朋友可能发现在三个平台上执行的程序都使用了一种模式的地址,如:
1、tencent://message/?uin=88888888;腾讯自己实现的协议,只有QQ可以解析这个命令具体要做什么
2、tel:15899996666;系统实现的协议,APP调用后可以拨打电话
3、itms-services://?action=download-manifest&amp;url=http://192.168.0.100:8080/latest/testapp.plist  苹果自己实现的协议,调用后可以绕过AppStore从其他网站下载APP,并安装

大家比较熟悉的是http://www.baidu.com,但是上面的是什么东东呢?他们比较像,同样也是协议调用,模式为:协议://命令?参数名称=参数值,这些协议可以是系统默认支持的(http、tel),也可以是用户自定义的(tencent、itms-services),对于 Windows、Linux 和 OS X 操作系统都支持自定协议,自定义协议可以帮助用户在同一系统上实现应用A调用应用B,比如直接在浏览器可以调用QQ,APP用tel协议调用系统拨打电话功能。

下面附上各操作系统如何实现自定义协议:
A protocol is a method that is used to send, receive, and handle information over a connection. Common protocols viewed from the browser include http, ftp, and mailto. In order for you to view information sent over a specific protocol, it must be registered. Once registered, the protocol can then be handled by the program you specify, such as your browser or a 3rd party viewer. This means that a hyperlink ( e.g. foo://fred ) can use the handler for protocol foo to open the file named fred.

Contents [hide]
1 Registering an unsupported protocol
1.1 Windows
1.2 Linux
1.3 OS X
2 Redirecting a registered protocol



[edit]Registering an unsupported protocol
Mozilla products utilize protocols defined internally, as well as those defined by the operating system. You can add the ability to use an unsupported protocol by registering it. The OS-specific method of doing this is described below.

[edit]Windows
Create the registry .reg file, replacing foo with your unregistered protocol, and the path with whatever handler program you want to run. Then merge it into the Windows registry.


REGEDIT4

[HKEY_CLASSES_ROOT\foo]
@="URL:foo Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\foo\shell]

[HKEY_CLASSES_ROOT\foo\shell\open]

[HKEY_CLASSES_ROOT\foo\shell\open\command]
@="\"C:\\Program Files\\Application\\program.exe\" \"%1\""
See Registering an Application to a URL Protocol for additional information.

[edit]Linux
Registration is unnecessary. Simply associate whatever proto: with a program through Firefox: Example: Add the sip: protocol to launch kphone for VoIP calls in Firefox:

- Type about:config into the address bar
- Right-click create new boolean value: network.protocol-handler.external.sip and set to true
- Right-click create new boolean value: network.protocol-handler.warn-external.sip and set to false
- Right-click create new string value: network.protocol-handler.app.sip and set to /usr/bin/kphone
This will actually launch kphone. Not sure if it will dial though. That is untested :)

You can also optionally register the protocol with whatever window manager you are using. In KDE this is done through Control Center - KDE Components - File Associations. This step is usually unnecessary unless your window manager has a custom browser, such as konqueror.

[edit]OS X
Probably very similar to Linux (above).

[edit]Redirecting a registered protocol
If the protocol is already handled by the browser, you can specify what program will be used as a handler to open the file. To do this, add the pref:
network.protocol-handler.app.foo as a string with value C:\Program Files\Application\program.exe
Note: If the path or name is incorrect, the browser will display an error saying "protocol (foo) isn't associated with any program". (See bug 312953).

You may also need to use the following prefs, although this is uncertain:
network.protocol-handler.external.foo = true
network.protocol-handler.expose.foo = false

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值