proximity.html 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8"/>
  5. <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
  6. <meta name="HandheldFriendly" content="true"/>
  7. <meta name="MobileOptimized" content="320"/>
  8. <title>Hello H5+</title>
  9. <script type="text/javascript" src="../js/common.js"></script>
  10. <script type="text/javascript">
  11. var ws=null;
  12. document.addEventListener( "plusready", function(){
  13. //watchProximity();
  14. ws=plus.webview.currentWebview();
  15. // 页面隐藏时自动停止监听
  16. ws.addEventListener('hide',function(){
  17. watchStop();
  18. },false);
  19. }, false );
  20. function getProximity() {
  21. outSet( "获取设备当前距离传感器信息" );
  22. plus.proximity.getCurrentProximity( function ( d ) {
  23. outLine( "距离为:" + d );
  24. }, function ( e ) {
  25. outLine( "获取失败:" + e.message );
  26. } );
  27. }
  28. var id=null,bright=null;
  29. function watchProximity() {
  30. if ( id ) {
  31. return;
  32. }
  33. outSet( "监听设备距离传感器信息" );
  34. bright = plus.screen.getBrightness();
  35. id = plus.proximity.watchProximity( function ( d ) {
  36. outLine( "距离变化:"+d );
  37. plus.screen.setBrightness( (d<1)?0.01:bright );
  38. }, function ( e ) {
  39. plus.proximity.clearWatch( id );
  40. id = null;
  41. outLine( "监听失败:" + e.message );
  42. } );
  43. }
  44. function watchStop() {
  45. if ( id ) {
  46. outSet( "停止监听设备距离传感器信息" );
  47. plus.proximity.clearWatch( id );
  48. id = null;
  49. } else {
  50. outSet( "没有监听设备距离传感器" );
  51. }
  52. }
  53. </script>
  54. <link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8"/>
  55. </head>
  56. <body>
  57. <br/>
  58. <p class="des">距离传感器一般都在手机听筒的两侧或者是在手机听筒凹槽中,当有物体接近时(如将手机靠近耳朵接听或拨打电话),距离感应器可以检测并通知屏幕背景灯熄灭,拿开时再度点亮背景灯,这样可方便用户操作并节省电量。</p>
  59. <br/>
  60. <ul class="dlist">
  61. <li class="ditem" onclick="getProximity()">获取距离传感器信息</li>
  62. <li class="ditem" onclick="watchProximity()">监听距离传感器变化</li>
  63. <li class="ditem" onclick="watchStop()">停止监听</li>
  64. </ul>
  65. <div id="outpos"/>
  66. <div id="output">
  67. Proximity模块管理设备距离传感器,可获取当前设备的接近距离信息,通过plus.proximity可获取设备距离传感管理对象。
  68. </div>
  69. </body>
  70. </html>