PhoneGap学习笔记(二) 部分事件、设备信息、网络状态和通知

部分事件如下:

deviceready:表示phoneGap已经就绪

menubutton:菜单键按下

backbutton:返回键按下

pause:程序转入后台运行

resume:程序进入前台运行

online:设备连接网络(测试设备从断网进入联网状态触发,WIFI和数据网络相互切换也会触发)

offline:设备断开网络(测试设备失去网络连接触发,WIFI和数据网络相互切换也会触发)

部分对象如下:

window.device:设备信息对象

device.name:设备名称

device.cordova:phonegap版本

device.platform:平台(Android、iOS、Windows 8、....)

device.uuid:设备编号

device.version:系统版本

检测网络类型:

var netType=navigator.network.connection.type;

其值分别是:

Connection.UNKNOWN

Connection.WIFI

Connection.CELL_2G

Connection.CELL_3G

Connection.CELL_4G

Connection.ETHERNET

Connection.NONE

综合示例如下:

 1 <!doctype html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5 <title>phonegap</title>
 6 <script src="js/cordova.js"></script>
 7 </head>
 8 
 9 <body>
10 <div>Hello Word</div>
11 <div id="msg"></div>
12 <div id="internet"></div>
13 <div id="device"></div>
14 <div id="conn"></div>
15 <script>
16 document.addEventListener("deviceready",function(){
17       init();
18 },false);
19 function init(){
20   var msg=document.getElementById("msg");
21   var internet=document.getElementById("internet");
22   var device=document.getElementById("device");
23   checkNetwordType();
24   document.addEventListener("backbutton",function(){
25       msg.innerHTML="返回键按下";
26   },false);
27   document.addEventListener("menubutton",function(){
28       msg.innerHTML="菜单键按下";  
29   },false);
30   document.addEventListener("pause",function(){
31       msg.innerHTML="程序转入后台运行";  
32   },false);
33   document.addEventListener("resume",function(){
34       msg.innerHTML="程序进入前台运行";
35   },false);
36   document.addEventListener("online",function(){
37       internet.innerHTML="设备已连接网络";
38       checkNetwordType();
39   },false);
40   document.addEventListener("offline",function(){
41       internet.innerHTML="设备已断开网络";
42       checkNetwordType();
43   },false);
44   var deviceInfo=window.device;
45   device.innerHTML="设备名称:"+deviceInfo.name+"<br/>phoneGap版本:"+deviceInfo.cordova+"<br/>设备类型:"+deviceInfo.platform+
46                    "<br/>设备编号:"+deviceInfo.uuid+"<br/>系统版本:"+deviceInfo.version;
47 }
48 function checkNetwordType(){
49   var conn=document.getElementById("conn");
50   var netType=navigator.network.connection.type;
51   var state={};
52   state[Connection.WIFI]="WIFI网络";
53   state[Connection.UNKNOWN]="网络未知";
54   state[Connection.CELL_2G]="2G网络";
55   state[Connection.CELL_3G]="3G网络";
56   state[Connection.CELL_4G]="4G网络";
57   state[Connection.ETHERNET]="以太网络";
58   state[Connection.NONE]="网络未连接";
59   conn.innerHTML=state[netType];    
60 }
61 </script>
62 </body>
63 </html>

 对话框

警告对话框:navigator.notification.alert("返回键按下",function(){},"对话框标题","确定");

四个参依次是:显示内容、回调函数、对话框标题和按钮

前两个参数必须,后面两个可省略

确认对话框:navigator.notification.confirm("确定删除?",callbak,"退出程序","确定,取消,保持现状");

四个参数前三个同警告对话框,第四个参数为按钮标题,可配置多个按钮,以","分隔,在callbak回调方法中捕获点击按钮

navigator.notification.confirm("确定删除?",callbak,"退出程序","确定,取消,保持现状");

function callbak(button){
  if(button==1){
      navigator.notification.alert("点击了确定按钮,退出程序");
  }else if(button==2){
      navigator.notification.alert("点击了取消按钮");
  }else if(button==3){
      navigator.notification.alert("点击了第三个按钮");
  }
}

使用鸣叫,调用系统通知音  

navigator.notification.beep(1);

测试该方法会阻塞js,只有系统鸣叫结束后才会执行下一行代码

使用系统震动

navigator.notification.vibrate(1000);,参数为震动时间,单位ms

原文地址:https://www.cnblogs.com/wangjiajun/p/4048266.html