【问题标题】:App doesn't broadcast when wakeLocks added添加唤醒锁时应用程序不广播
【发布时间】:2017-10-21 13:34:18
【问题描述】:

我有一个用于 Firefox OS 设备(ZTE Open v1.1)的 geoLocation 应用程序,它向网络服务器广播其位置详细信息。

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

我认为将以下 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();

【问题讨论】:

    标签: firefox-os


    【解决方案1】:

    你试过了吗?

    navigator.requestWakeLock('cpu');
    

    如果这仍然不起作用,也许您应该关注以下讨论: Firefox OS Background services

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-09
      • 2016-05-14
      • 1970-01-01
      相关资源
      最近更新 更多