PhoneGap-----Device Infomation、Network State and four kinds of Notifications

本文介绍了如何利用PhoneGap框架获取设备基本信息、检查网络状态及实现不同类型的提示通知。通过简单的HTML与JavaScript代码示例,展示了如何在移动端应用中集成这些功能。

    The first , we can lead how to get the device Infomation ! It's very convenient by Phonegap ! At least , I think so !

<!DOCTYPE html>
<html >
<head>
<title>My Device</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">

   function onLoad()
   {
	   document.addEventListener("deviceready",onDeviceReady,false);
   }
   
   function onDeviceReady()
   {
	   var myDiv = document.getElementById('props');
	   myDiv.innerHTML = 'Device Name:    ' + device.name     + '<br />' + 
	                     'Device PhoneGap:' + device.phonegap + '<br />' + 
						 'Device Platform:' + device.platform + '<br />' +
						 'Device UUID:    ' + device.uuid     + '<br />' +
						 'Device Version: ' + device.version  + '<br />' ;
   }
   
</script>
</head>
<body onLoad="onLoad()">
   <p id="props">Loading device properties...</p>
</body>
</html>

    The second , we can check the network state : 

<!DOCTYPE html>
<html >
<head>
<title>Connectivity Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">

   document.addEventListener("deviceready",onDeviceReady,false);
   
   function onDeviceReady()
   {
	  checkConnection();
   }
   
   function checkConnection()
   {
	   var networkState = navigator.network.connection.type;
	   
	   var states = {};
	   states[Connection.UNKNOWN] = 'Unknown connection';
	   states[Connection.ETHERNET] = 'Ethernet connection';
	   states[Connection.WIFI] = 'Wifi connection';
	   states[Connection.CELL_2G] = 'Cell 2G connection';
	   states[Connection.CELL_3G] = 'Cell 3G connection';
	   states[Connection.CELL_4G] = 'Cell 4G connection';
	   states[Connection.NONE] = 'No network connection';
	   
	   alert('Connection type:' + states[networkState]);
   }
   
</script>
</head>
<body>
   <p>A dialog box will report the network state.</p>
</body>
</html>

    The third , the cases of four kinds of notifications : 

<!DOCTYPE html>
<html>
<head>
<title>PhoneGap Event Example</title>

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">

   function onLoad()
   {
	   document.addEventListener("deviceready",onDeviceReady,false);
   }
   
   function onDeviceReady(){
   }
   
   function showAlert(){
	   navigator.notification.alert(
	     'Game Over!',//message
		 alertCallback,//callback
		 'Game Over',
		 'Done'//buttonName
		 );
   }
   
   function alertCallback(){
   }
   
   function onConfirm(button){
	   alert('You selected button ' + button);
   }
   
   function showConfirm(){
	   navigator.notification.confirm(
	     'Game Over!',
		 onConfirm,
		 'Game Over',
		 'Restart,Exit'
		 );
   }
   
   function playBeep(){
	   navigator.notification.beep(2);
   }
   
   function vibrate(){
	   navigator.notification.vibrate(4000);
   }
   
</script>
</head>
<body onLoad="onLoad()">
   <p><a href="#" onClick="showAlert();return false;">Show Alert</a></p>
   <p><a href="#" onClick="showConfirm();return false;">Show Confirmation</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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值