PhoneGap API介绍:Notification

来源:互联网 发布:mac制谱软件 编辑:程序博客网 时间:2024/04/30 14:21

方法

  • notification.alert
  • notification.confirm
  • notification.beep
  • notification.vibrate



      显示一个定制的警告或对话框。

  1. navigator.notification.alert(message, alertCallback, [title], [buttonName]); 
  • message:对话框信息。(字符串类型)
  • alertCallback:当警告对话框被忽略时调用的回调函数。(函数类型)
  • title:对话框标题。(字符串类型)(可选项,默认值为“Alert”)
  • buttonName:按钮名称(字符串类型)(可选项,默认值为“OK”)

说明

  • 大多数PhoneGap使用本地对话框实现该功能。然而,一些平台只是简单的使用浏览器的alert函数,而这种方法通常是不能定制的。

支持的平台

  • Android
  • BlackBerry (OS 4.6)
  • BlackBerry WebWorks (OS 5.0或更高版本)
  • iPhone

简单的范例

  1. // Android / BlackBerry WebWorks (OS 5.0 and higher) // iPhone 
  2. function alertDismissed() { 
  3.     // 进行处理 
  4.  
  5. navigator.notification.alert( 
  6.     'You are the winner!',  // 显示信息 
  7.        alertDismissed,         // 警告被忽视的回调函数 
  8.        'Game Over',            // 标题 
  9.        'Done'                  // 按钮名称 
  10. ); 
  11.  
  12. // BlackBerry (OS 4.6) // webOS 
  13. navigator.notification.alert('You are the winner!'); 

完整的范例

  1. <!DOCTYPE html> 
  2. <html> 
  3. <head>     
  4. <title>Notification Example</title> 
  5.  
  6. <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> 
  7. <script type="text/javascript" charset="utf-8"> 
  8.  
  9.     // 等待加载PhoneGap 
  10.     document.addEventListener("deviceready", onDeviceReady, false); 
  11.      
  12.     // PhoneGap加载完毕 
  13.     function onDeviceReady() { 
  14.         // 空 
  15.     } 
  16.      
  17.     // 警告对话框被忽视 
  18.     function alertDismissed() { 
  19.         // 进行处理 
  20.     } 
  21.      
  22.     // 显示一个定制的警告框 
  23.     function showAlert() { 
  24.         navigator.notification.alert( 
  25.             'You are the winner!',  // 显示信息 
  26.             alertDismissed,         // 警告被忽视的回调函数 
  27.             'Game Over',            // 标题 
  28.             'Done'                  // 按钮名称 
  29.         ); 
  30.     } 
  31.  
  32. </script> 
  33. </head> 
  34. <body> 
  35.     <p><a href="#" onclick="showConfirm(); return false;">Show Confirm</a></p> 
  36. </body> 
  37. </html> 

notification.confirm

  • 显示一个可定制的确认对话框。
  1. navigator.notification.confirm(message, confirmCallback, [title], [buttonLabels]); 
  • message:对话框信息。(字符串类型)
  • confirmCallback:按下按钮后触发的回调函数,返回按下按钮的索引(1、2或3)。(函数类型)
  • title:对话框标题。(字符串类型)(可选项,默认值为“Confirm”)
  • buttonLabels:逗号分隔的按钮标签字符串。(字符串类型)(可选项,默认值为“OK、Cancel”)

说明

  • notification.confirm函数显示一个定制性比浏览器的confirm函数更好的本地对话框。

支持的平台

  • Android
  • BlackBerry WebWorks (OS 5.0或更高版本)
  • iPhone

简单的范例

  1. // 处理确认对话框返回的结果 
  2. function onConfirm(button) { 
  3.     alert('You selected button ' + button); 
  4.  
  5. // 显示一个定制的确认对话框 
  6. function showConfirm() { 
  7.        navigator.notification.confirm( 
  8.            'You are the winner!',  // 显示信息 
  9.            onConfirm,              // 按下按钮后触发的回调函数,返回按下按钮的索引 
  10.            'Game Over',            // 标题 
  11.            'Restart,Exit'          // 按钮标签 
  12.        ); 
  13.  } 

完整的范例

  1. <!DOCTYPE html> 
  2. <html> 
  3. <head>     
  4. <title>Notification Example</title> 
  5.  
  6. <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> 
  7. <script type="text/javascript" charset="utf-8"> 
  8.      
  9.     // 等待加载PhoneGap 
  10.     document.addEventListener("deviceready", onDeviceReady, false); 
  11.      
  12.     // PhoneGap加载完毕 
  13.     function onDeviceReady() { 
  14.         // 空 
  15.     } 
  16.      
  17.     // 处理确认对话框返回的结果 
  18.     function onConfirm(button) { 
  19.         alert('You selected button ' + button); 
  20.     } 
  21.      
  22.     // 显示一个定制的确认对话框 
  23.     function showConfirm() { 
  24.         navigator.notification.confirm( 
  25.             'You are the winner!',  // 显示信息 
  26.             onConfirm,              // 按下按钮后触发的回调函数,返回按下按钮的索引     
  27.             'Game Over',            // 标题 
  28.             'Restart,Exit'          // 按钮标签 
  29.         ); 
  30.     } 
  31.  
  32. </script> 
  33. </head> 
  34. <body> 
  35.     <p><a href="#" onclick="showConfirm(); return false;">Show Confirm</a></p> 
  36. </body> 
  37. </html> 
注解:
简单的解释下:ios6以上alert()会出现点击首次没有相应,是cordova自身的bug.最好用本地代码 实现

原创粉丝点击