我有一个应用程序每 5 秒发送一次地理位置,如果没有找到新位置,则每分钟最后一次找到位置。所以基本上应用程序无法停止将地理位置发送到 PHP 文件。
尽管如此,它确实如此。完全随机的。该应用程序只是停止发送到服务器而没有错误(因为我的 onerror 中有一个警报)并且当我打开应用程序以查看发生了什么时才开始重新发送。
顺便说一句,它似乎在 Android 上运行良好!
我的 info.plist 中有一些东西可以让它保持活力(它确实让它保持活力,但我猜只是随机停止生活):
<key>UIBackgroundModes</key>
<array>
<string>external-accessory</string>
<string>location</string>
</array>
我的 httprequest 看起来像这样:
function sendCoordinates() {
//Reset the visual text(errors/succesmessage etc)
if (Titanium.Network.online) {
//Concat the GPSholder array into the toSend and than empty the GPSholder.
//To toSend accumulates GPSholder arrays in case it can't be sent for some reason but avoids getting duplicates in the GPSholder
//the toSend is emptied out after a succesful save.
toSend = toSend.concat(getGPSholder());
GPSholder = [];
if (toSend.length > 0) {
GPSSaved.text = '';
minuteInterval = 0;
var xhr=Titanium.Network.createHTTPClient({enableKeepAlive: false});
xhr.open("POST","http://xxx.nl/website/services/esrm_tracker/push_tt_positions.php");
xhr.onload = function(){
if(this.status == '200'){
if(this.readyState == 4){
var result = JSON.parse(this.responseText);
switch(result.result) {
case 1:
secondsLastSent = 0;
counterBlock.text = "De laatste locatie is " + secondsLastSent + " seconden geleden verstuurd";
counterBlock.show();
toSend = [];
break;
case -1:
GPSSaved.text = 'Authorisatie code niet geldig. Er worden geen locaties meer verstuurd.';
GPSstop();
break;
case -2:
GPSSaved.text = 'Locaties niet geldig';
break;
case -3:
GPSSaved.text = 'Authorisatie code niet gevonden. Er worden geen locaties meer verstuurd.';
GPSstop();
break;
case -10:
GPSstop();
break;
default:
GPSSaved.text = 'Onbekende fout. Er worden geen locaties meer verstuurd.';
GPSstop();
break;
}
}
}
}
xhr.onerror = function(e){
GPSSaved.text = e.status + ' <- error';
alert(e);
};
xhr.timeout = 10000;
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
var str = JSON.stringify(toSend);
var params = {
auth_key : auth_key,
locations : str
};
xhr.send(params);
}
} else {
GPSSaved.text = 'Geen internet. Het versturen van locaties wordt hervat als de verbinding is hervat.';
}
}