在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ HTML/ Cordova加速度傳感器
Cordova國際化
Cordova設(shè)備信息
Cordova教程
Cordova照相機(jī)
Cordova事件
Cordova config.xml文件
Cordova聯(lián)系人
Cordova文件系統(tǒng)
Cordova Plugman
Cordova第一個(gè)應(yīng)用
Cordova存儲
Cordova文件傳輸
Cordova網(wǎng)絡(luò)信息
Cordova振動(dòng)
Cordova對話框
Cordova加速度傳感器
Cordova白名單
Cordova地理位置
Cordova設(shè)備方向
Cordova InAppBrowser打開Web瀏覽器
Cordova多媒體
Cordova開機(jī)畫面
Cordova環(huán)境安裝設(shè)置
Cordova視頻錄制
Cordova后退按鈕
Cordova電池狀態(tài)

Cordova加速度傳感器

該插件也被稱為設(shè)備運(yùn)動(dòng)。 它用來在三個(gè)維度來跟蹤設(shè)備的運(yùn)動(dòng)。

第1步 - 安裝加速度傳感器插件

我們將用cordova-CLI來安裝這個(gè)插件。在命令提示符窗口鍵入下面的代碼。

D:\worksp\cordova\CordovaProject>cordova plugin add cordova-plugin-device-motion

第2步 - 添加按鈕

我們需要做的下一件事就是在 index.html 文件中添加兩個(gè)按鈕。一個(gè)將被用于獲取當(dāng)前加速度并其他將觀察加速度的變化。

<body>
        <div class="app">
            <h1>Cordova加速度傳感器</h1>
            <button id = "getAcceleration">GET ACCELERATION</button>
            <button id = "watchAcceleration">WATCH ACCELERATION</button>

        </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
</body>

第3步 - 添加事件監(jiān)聽器

將按鈕添加到 index.js 中的事件偵聽器 onDeviceReady 函數(shù)內(nèi)部(放在前面)。
document.getElementById("getAcceleration").addEventListener("click", getAcceleration);
document.getElementById("watchAcceleration").addEventListener("click", watchAcceleration);

第4步 - 創(chuàng)建函數(shù)

我們將創(chuàng)建兩個(gè)函數(shù)。第一個(gè)將被用來獲取當(dāng)前加速度,第二個(gè)會看加速度并每隔三秒鐘告知我們。我們也通過添加clearWatch setTimeout函數(shù)包裝停止指定的時(shí)間幀之后監(jiān)視加速度。頻率(frequency)參數(shù)用于每三秒觸發(fā)回調(diào)函數(shù)。

function getAcceleration(){
   navigator.accelerometer.getCurrentAcceleration(accelerometerSuccess, accelerometerError);

   function accelerometerSuccess(acceleration) {
      alert('Acceleration X: ' + acceleration.x + '\n' +
         'Acceleration Y: ' + acceleration.y + '\n' +
         'Acceleration Z: ' + acceleration.z + '\n' +
         'Timestamp: '      + acceleration.timestamp + '\n');
   };

   function accelerometerError() {
      alert('onError!');
   };
	
}

function watchAcceleration(){
    
   var accelerometerOptions = {
      frequency: 3000
   }

   var watchID = navigator.accelerometer.watchAcceleration(accelerometerSuccess, accelerometerError, accelerometerOptions);

   function accelerometerSuccess(acceleration) {
      alert('Acceleration X: ' + acceleration.x + '\n' +
         'Acceleration Y: ' + acceleration.y + '\n' +
         'Acceleration Z: ' + acceleration.z + '\n' +
         'Timestamp: '      + acceleration.timestamp + '\n');

      setTimeout(function() {
         navigator.accelerometer.clearWatch(watchID);
      }, 10000);

   };

   function accelerometerError() {
      alert('onError!');
   };
	
} 

現(xiàn)在,如果我們按GET ACCELERATION按鈕將獲得當(dāng)前加速度值。如果我們按WATCH ACCELERATION警告提示將每三秒鐘觸發(fā)。第三警告提示顯示后,clearWatch函數(shù)將被調(diào)用,因?yàn)槲覀冊O(shè)置超時(shí)時(shí)間為10000毫秒,所以不會得到任何更多的警報(bào)。