正如在 Mozilla 的 Boot to Gecko - 网页就是平台 和 Gaia,Mozilla 为 Boot to Gecko 提供的用户界面 中所讨论和展示的那样,网页正在成为一个非常强大的平台!因此,我想向您介绍我们 WebAPI 计划中的两个激动人心的 API:WebTelephony 和 WebSMS。
WebTelephony
访问电话功能的基础很简单,只需通过 navigator.mozTelephony。一旦您获得了对该对象的引用,您就可以开始拨打和接听电话。以下是一些示例
// Telephony object
var tel = navigator.mozTelephony;
// Check if the phone is muted (read/write property)
console.log(tel.muted);
// Check if the speaker is enabled (read/write property)
console.log(tel.speakerEnabled);
// Place a call
var call = tel.dial("123456789");
// Events for that call
call.onstatechange = function (event) {
/*
Possible values for state:
"dialing", "ringing", "busy", "connecting", "connected",
"disconnecting", "disconnected", "incoming"
*/
console.log(event.state);
};
// Above options as direct events
call.onconnected = function () {
// Call was connected
};
call.ondisconnected = function () {
// Call was disconnected
};
// Receiving a call
tel.onincoming = function (event) {
var incomingCall = event.call;
// Get the number of the incoming call
console.log(incomingCall.number);
// Answer the call
incomingCall.answer();
};
// Disconnect a call
call.hangUp();
// Iterating over calls, and taking action depending on their changed status
tel.oncallschanged = function (event) {
tel.calls.forEach(function (call) {
// Log the state of each call
console.log(call.state);
});
};
Telephony 目前在 Gaia 的拨号器和主屏幕中可用。
WebSMS
手机中另一项核心功能是发送和接收短信。以下是如何做到这一点
// SMS object
var sms = navigator.mozSMS;
// Send a message
sms.send("123456789", "Hello world!");
// Recieve a message
sms.onrecieved = function (event) {
// Read message
console.log(event.message);
};
黑客和贡献
如果您有兴趣更深入地了解其内部工作原理,我建议您查看 Mozilla 为 Boot to Gecko 提供的用户界面,Gaia。在那里,您可以查看 dialer.js 文件和 sms.js 文件。
如果您认为也可以使用您的网页技术技能来开发和定制手机,请不要犹豫,查看并为 Gaia 贡献力量!
关于 Robert Nyman [名誉编辑]
Mozilla Hacks 的技术布道者和编辑。就 HTML5、JavaScript 和开放网页发表演讲和博客文章。Robert 是 HTML5 和开放网页的坚定支持者,自 1999 年以来一直在从事网页前端开发工作 - 在瑞典和纽约市。他还经常在 http://robertnyman.com 上发表博客文章,喜欢旅行和结识新朋友。
12 条评论