PhoneGap API帮助文档翻译Notification提醒是本文要介绍的内容,主要是来了解PhoneGap API文档的内容,具体PhoneGap API文档内容的详解来看本文,设备的视觉、听觉和触觉通知。
方法:
notification.alert
notification.confirm
notification.beep
notification.vibrate
notification.alert
- 1.
- 2.
- 3.
- 4.
- 5.
显示一个定制的警告或对话框。
navigator.notification.alert(message, alertCallback, [title], [buttonName]);
navigator.notification.alert(message, alertCallback, [title], [buttonName]);
- 1.
- 2.
message:对话框信息。(字符串类型)
alertCallback:当警告对话框被忽略时调用的回调函数。(函数类型)
title:对话框标题。(字符串类型)(可选项,默认值为“Alert”)
buttonName:按钮名称(字符串类型)(可选项,默认值为“OK”)
说明:
大多数PhoneGap使用本地对话框实现该功能。然而,一些平台只是简单的使用浏览器的alert函数,而这种方法通常是不能定制的。
支持的平台:
Android
BlackBerry (OS 4.6)
BlackBerry WebWorks (OS 5.0或更高版本)
iPhone
简单的范例:
// Android / BlackBerry WebWorks (OS 5.0 and higher) // iPhone
function alertDismissed() {
// 进行处理
}
navigator.notification.alert(
'You are the winner!', // 显示信息
alertDismissed, // 警告被忽视的回调函数
'Game Over', // 标题
'Done' // 按钮名称
);
// BlackBerry (OS 4.6) // webOS
navigator.notification.alert('You are the winner!');
// Android / BlackBerry WebWorks (OS 5.0 and higher) // iPhone
function alertDismissed() {
// 进行处理
}
navigator.notification.alert(
'You are the winner!', // 显示信息
alertDismissed, // 警告被忽视的回调函数
'Game Over', // 标题
'Done' // 按钮名称
);
// BlackBerry (OS 4.6) // webOS
navigator.notification.alert('You are the winner!');
完整的范例:
view plaincopy to clipboardprint?<!DOCTYPE html>
<html>
<head>
<title>Notification Example</title>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
// 等待加载PhoneGap
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap加载完毕
function onDeviceReady() {
// 空
}
// 警告对话框被忽视
function alertDismissed() {
// 进行处理
}
// 显示一个定制的警告框
function showAlert() {
navigator.notification.alert(
'You are the winner!', // 显示信息
alertDismissed, // 警告被忽视的回调函数
'Game Over', // 标题
'Done' // 按钮名称
);
}
</script>
</head>
<body>
<p><a href="#" onclick="showConfirm(); return false;">Show Confirm</a></p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Notification Example</title>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
// 等待加载PhoneGap
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap加载完毕
function onDeviceReady() {
// 空
}
// 警告对话框被忽视
function alertDismissed() {
// 进行处理
}
// 显示一个定制的警告框
function showAlert() {
navigator.notification.alert(
'You are the winner!', // 显示信息
alertDismissed, // 警告被忽视的回调函数
'Game Over', // 标题
'Done' // 按钮名称
);
}
</script>
</head>
<body>
<p><a href="#" onclick="showConfirm(); return false;">Show Confirm</a></p>
</body>
</html>
notification.confirm
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
显示一个可定制的确认对话框。
navigator.notification.confirm(message, confirmCallback, [title], [buttonLabels]);
navigator.notification.confirm(message, confirmCallback, [title], [buttonLabels]);
- 1.
- 2.
message:对话框信息。(字符串类型)
confirmCallback:按下按钮后触发的回调函数,返回按下按钮的索引(1、2或3)。(函数类型)
title:对话框标题。(字符串类型)(可选项,默认值为“Confirm”)
buttonLabels:逗号分隔的按钮标签字符串。(字符串类型)(可选项,默认值为“OK、Cancel”)
#p#
PhoneGap API说明:
notification.confirm函数显示一个定制性比浏览器的confirm函数更好的本地对话框。
支持的平台:
Android
BlackBerry WebWorks (OS 5.0或更高版本)
iPhone
简单的范例:
// 处理确认对话框返回的结果
function onConfirm(button) {
alert('You selected button ' + button);
}
// 显示一个定制的确认对话框
function showConfirm() {
navigator.notification.confirm(
'You are the winner!', // 显示信息
onConfirm, // 按下按钮后触发的回调函数,返回按下按钮的索引
'Game Over', // 标题
'Restart,Exit' // 按钮标签
);
}
// 处理确认对话框返回的结果
function onConfirm(button) {
alert('You selected button ' + button);
}
// 显示一个定制的确认对话框
function showConfirm() {
navigator.notification.confirm(
'You are the winner!', // 显示信息
onConfirm, // 按下按钮后触发的回调函数,返回按下按钮的索引
'Game Over', // 标题
'Restart,Exit' // 按钮标签
);
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
完整的范例:
<!DOCTYPE html>
<html>
<head>
<title>Notification Example</title>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
// 等待加载PhoneGap
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap加载完毕
function onDeviceReady() {
// 空
}
// 处理确认对话框返回的结果
function onConfirm(button) {
alert('You selected button ' + button);
}
// 显示一个定制的确认对话框
function showConfirm() {
navigator.notification.confirm(
'You are the winner!', // 显示信息
onConfirm, // 按下按钮后触发的回调函数,返回按下按钮的索引
'Game Over', // 标题
'Restart,Exit' // 按钮标签
);
}
</script>
</head>
<body>
<p><a href="#" onclick="showConfirm(); return false;">Show Confirm</a></p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Notification Example</title>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
// 等待加载PhoneGap
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap加载完毕
function onDeviceReady() {
// 空
}
// 处理确认对话框返回的结果
function onConfirm(button) {
alert('You selected button ' + button);
}
// 显示一个定制的确认对话框
function showConfirm() {
navigator.notification.confirm(
'You are the winner!', // 显示信息
onConfirm, // 按下按钮后触发的回调函数,返回按下按钮的索引
'Game Over', // 标题
'Restart,Exit' // 按钮标签
);
}
</script>
</head>
<body>
<p><a href="#" onclick="showConfirm(); return false;">Show Confirm</a></p>
</body>
</html>
notification.beep
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
设备将发出蜂鸣声。
navigator.notification.beep(times);
navigator.notification.beep(times);
- 1.
- 2.
times:蜂鸣声的重复次数。(数字类型)
支持的平台:
Android
BlackBerry (OS 4.6)
BlackBerry WebWorks (OS 5.0或更高版本)
iPhone
简单的范例:
// 蜂鸣2次!
navigator.notification.beep(2);
// 蜂鸣2次!
navigator.notification.beep(2);
- 1.
- 2.
- 3.
- 4.
完整的范例:
<!DOCTYPE html>
<html>
<head>
<title>Notification Example</title>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
// 等待加载PhoneGap
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap加载完毕
function onDeviceReady() {
// 空
}
// 显示一个定制的警告框
function showAlert() {
navigator.notification.alert(
'You are the winner!', // 显示信息
'Game Over', // 标题
'Done' // 按钮名称
);
}
// 蜂鸣三次
function playBeep() {
navigator.notification.beep(3);
}
// 震动两秒
function vibrate() {
navigator.notification.vibrate(2000);
}
</script>
</head>
<body>
<p><a href="#" onclick="showAlert(); return false;">Show Alert</a></p>
<p><a href="#" onclick="playBeep(); return false;">Play Beep</a></p>
<p><a href="#" onclick="vibrate(); return false;">Vibrate</a></p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Notification Example</title>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
// 等待加载PhoneGap
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap加载完毕
function onDeviceReady() {
// 空
}
// 显示一个定制的警告框
function showAlert() {
navigator.notification.alert(
'You are the winner!', // 显示信息
'Game Over', // 标题
'Done' // 按钮名称
);
}
// 蜂鸣三次
function playBeep() {
navigator.notification.beep(3);
}
// 震动两秒
function vibrate() {
navigator.notification.vibrate(2000);
}
</script>
</head>
<body>
<p><a href="#" onclick="showAlert(); return false;">Show Alert</a></p>
<p><a href="#" onclick="playBeep(); return false;">Play Beep</a></p>
<p><a href="#" onclick="vibrate(); return false;">Vibrate</a></p>
</body>
</html>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
#p#
Android的特异情况:
Androids会播放在“设置/音效及显示”面板中指定的默认“通知铃声”。
iPhone的特异情况:
忽略蜂鸣次数参数。
iPhone没有本地的蜂鸣API。
PhoneGap通过多媒体API播放音频文件来实现蜂鸣。
用户必须提供一个包含所需的蜂鸣声的文件。
此文件播放时长必须短于30秒,位于www/root,并且必须命名为beep.wav。
notification.vibrate
使设备震动指定的时长。
navigator.notification.vibrate(milliseconds);
navigator.notification.vibrate(milliseconds);
- 1.
- 2.
time:以毫秒为单位的设备震动时长,1000毫秒为1秒。(数字类型)
支持的平台:
Android
BlackBerry (OS 4.6)
BlackBerry WebWorks (OS 5.0或更高版本)
iPhone
简单的范例:
// 震动2.5秒
navigator.notification.vibrate(2500);
// 震动2.5秒
navigator.notification.vibrate(2500);
- 1.
- 2.
- 3.
- 4.
完整的范例:
<!DOCTYPE html>
<html>
<head>
<title>Notification Example</title>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
// 等待加载PhoneGap
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap加载完毕
function onDeviceReady() {
//空
}
// 显示定制警告框
function showAlert() {
navigator.notification.alert(
'You are the winner!', // 显示信息
'Game Over', // 标题
'Done' // 按钮名称
);
}
// 响三次
function playBeep() {
navigator.notification.beep(3);
}
// 震动两秒
function vibrate() {
navigator.notification.vibrate(2000);
}
</script>
</head>
<body>
<p><a href="#" onclick="showAlert(); return false;">Show Alert</a></p>
<p><a href="#" onclick="playBeep(); return false;">Play Beep</a></p>
<p><a href="#" onclick="vibrate(); return false;">Vibrate</a></p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Notification Example</title>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
// 等待加载PhoneGap
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap加载完毕
function onDeviceReady() {
//空
}
// 显示定制警告框
function showAlert() {
navigator.notification.alert(
'You are the winner!', // 显示信息
'Game Over', // 标题
'Done' // 按钮名称
);
}
// 响三次
function playBeep() {
navigator.notification.beep(3);
}
// 震动两秒
function vibrate() {
navigator.notification.vibrate(2000);
}
</script>
</head>
<body>
<p><a href="#" onclick="showAlert(); return false;">Show Alert</a></p>
<p><a href="#" onclick="playBeep(); return false;">Play Beep</a></p>
<p><a href="#" onclick="vibrate(); return false;">Vibrate</a></p>
</body>
</html>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
iPhone的特异情况:
time:忽略时长参数,震动时长为预先设定值。
简单的范例:
<span style="font-size:13px;">navigator.notification.vibrate();
navigator.notification.vibrate(2500); // 2500被忽略掉</span>
- 1.
- 2.
小结:PhoneGap API帮助文档翻译Notification提醒的内容介绍完了,希望通过:PhoneGap API文档内容的学习能对你有所帮助!