首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加wakeLocks时,应用程序不广播

添加wakeLocks时,应用程序不广播
EN

Stack Overflow用户
提问于 2017-10-21 21:34:19
回答 1查看 137关注 0票数 1

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

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

我以为将以下requestWakeLocks添加到代码中可以解决这个问题,但它似乎没有帮助:

代码语言:javascript
复制
var lock = window.navigator.requestWakeLock('gps'); 
var lock = window.navigator.requestWakeLock('wifi'); 

你知道我做错了什么吗?

代码:

代码语言:javascript
复制
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();
EN

回答 1

Stack Overflow用户

发布于 2018-02-06 11:44:52

你试过了吗?

代码语言:javascript
复制
navigator.requestWakeLock('cpu');

如果这仍然不起作用,也许您应该遵循下面的讨论:Firefox OS Background services

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46863789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档