【发布时间】:2016-05-02 09:00:33
【问题描述】:
我在 cordova 上创建了一个应用程序,这是一个在地图上寻找朋友的社交网络。当应用程序处于前台时(使用 cordova-plugin-geolocation 插件),地理定位工作正常。 但我也希望它在应用程序在后台运行时工作。为此,我使用了cordova-plugin-background-mode,以这种方式工作:
var geolocID;
window.onload = function(){
document.addEventListener("deviceready", init, false);
document.addEventListener("resume", function() { console.log("resume with id :" + geolocID); navigator.geolocation.clearWatch(geolocID); init();},false);
document.addEventListener("pause", geolocBackground,false);//function() {setTimeout(function(){console.log("TEST");},5000)},false);
}
function geolocBackground()
{
console.log("In geolocaBackground " + ", ID = " + geolocID);
navigator.geolocation.clearWatch(geolocID);
console.log("ID = " + geolocID);
geolocID = navigator.geolocation.watchPosition(positionSuccessBackground,positionError,{timeout: 5000});
console.log("Geolocation end");
}
function init()
{
geolocID = navigator.geolocation.watchPosition(positionSuccess, positionError, {enableHighAccuracy: true, timeout: 60000, frequency: 10000});
cordova.plugins.backgroundMode.enable();
cordova.plugins.backgroundMode.onactivate = function(){console.log("Backgound mode activated");
cordova.plugins.backgroundMode.configure({silent: false});
};
cordova.plugins.backgroundMode.ondeactivate = function() {console.log("Backgound mode disabled");};
}
backgkound 模式似乎有效,因为当我暂停应用程序时,我的控制台中有这个:
In geolocaBackground , ID = 707e54f8-fb99-fb36-2855-341b45f3d774
ID = 707e54f8-fb99-fb36-2855-341b45f3d774
Geolocation end
resume with id :707e54f8-fb99-fb36-2855-341b45f3d774
但是地理定位不起作用,返回超时错误,代码 3.. 我不明白我的代码有什么问题,我在网上没有找到任何东西。貌似app无法在后台获取手机的位置信息。代码已执行,但只有定位功能不起作用。如果有人对此问题有所了解,将不胜感激:) 感谢您的帮助,问候
【问题讨论】:
标签: cordova plugins background location timeout