2017-10-21 56 views
1

我有一个适用于Firefox OS设备(ZTE Open v1.1)的地理位置应用程序,它向Web服务器广播其位置详细信息。当wakeLocks添加时,应用程序不会广播

但是,如果我最小化应用程序并关闭屏幕,我的代码不会运行。

我想加入以下requestWakeLocks的代码将排序的问题,但它似乎并没有帮助:

var lock = window.navigator.requestWakeLock('gps'); 
var lock = window.navigator.requestWakeLock('wifi'); 

你有任何想法,我做错了什么?

代码:

function init() { 
    document.addEventListener("DOMContentLoaded", watchPosition, false); // to test on web browser 
    //document.addEventListener("deviceready", watchPosition, false); // deviceready is a cordova event 
} 

/* ---------------------------------- Local Variables ---------------------------------- */ 
var checkPeriodically; 
var watchPositionOutput = document.getElementById('watchPositionOutput'); 
var ajaxVars; // HTTP POST data values 

/* ---------------------------------- Local Functions ---------------------------------- */ 
function watchPosition() { 

    var lock = window.navigator.requestWakeLock('gps'); // FireFox-OS only - keeps the gps active when screen is off 
    var lock = window.navigator.requestWakeLock('wifi'); 
    checkPeriodically = setInterval(checkTime, 10000); 
    var watchID = navigator.geolocation.watchPosition(onSuccess, onError, options); 

    var options = { 
     enableHighAccuracy: true, 
    } 

    function onSuccess(position) { 
     ajaxVars = 
      "lt=" +  position.coords.latitude + 
      "&lg=" + position.coords.longitude + 
      "&ac=" + position.coords.accuracy + 
      "&sp=" + position.coords.speed + 
      "&ts=" + position.timestamp + 
      "&sec=SEC_TOKEN"; 

     var dt = new Date(position.timestamp); 
     date_time = 
      dt.getFullYear() + '-' + 
      (dt.getMonth() + 1) + '-' + 
      dt.getDate() + ' ' + 
      dt.getHours() + ':' + 
      dt.getMinutes() + ':' + 
      dt.getSeconds(); 

     watchPositionOutput.innerHTML = 
      'Latitude: ' + position.coords.latitude + '<br>' + 
      'Longitude: ' + position.coords.longitude + '<br>' + 
      'Accuracy: ' + position.coords.accuracy + '<br>' + 
      'Speed: '  + position.coords.speed  + '<br>' + 
      'Timestamp: ' + date_time     + '<br>'; 
    } 

    function onError(error) { 
     watchPositionOutput.innerHTML = 'code: ' + error.code + '<br>' +'message: ' + error.message + '<br>'; 
    } 

} 

// update the server with location data 
function ajax_post(postData){ 
    // when there is no data in postData 
    if (typeof(postData) === 'undefined') { return false; } // exit the function 

    var req = new XMLHttpRequest(); // Create XMLHttpRequest object 

    var url = "http://example.com/locate-me/post.php"; 
    //var url = "http://localhost/locate-me/post.php"; 

    req.open("POST", url, true); 

    // Set content type header info for sending url encoded variables in the request 
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 

    // Access the onreadystatechange event for the XMLHttpRequest object 
    req.onreadystatechange = function() { 
     if(req.readyState == 4 && req.status == 200) { 
      var return_data = req.responseText; // return whatever php echos 
      var date_time = new Date(return_data * 1000); // php is currently returning the time (timestamp) 
      document.getElementById("status").innerHTML = "Server time: " + date_time; 
     } 
    } 
    // Send data to PHP, and wait for response to update the status div 
    req.send(postData); // execute the request 
    document.getElementById("status").innerHTML = "processing..."; 
} 

// schedule to post the position data to a php script during certain times on certain days 
function checkTime(){ 
    // for example a day (day 0 == Sun) between 06:00 abd 23:45 
    var d = new Date(); 
    var today = d.getDay(); 
    var hms = d.getHours()+":"+d.getMinutes(); 
    // mon - thurs 
    if((today === 1 || today === 2 || today === 3 || today === 4) && hms > "10:23" && hms < "15:40") { 
     ajax_post(ajaxVars); 
    } 
    // friday 
    else if(today === 5 && hms > "13:00" && hms < "13:40") { 
     ajax_post(ajaxVars); 
    } 
    // testing: run all the time 
    else if(today < 7) { 
     ajax_post(ajaxVars); 
    } 
    else { 
     document.getElementById("status").innerHTML = "Data not scheduled to be posted to the server yet"; 
    } 
} 

init(); 

回答

相关问题