javascript - 在后台服务中发送地理定位[钛]

标签 javascript ios titanium titanium-mobile background-process

我正在使用 Titan 创建一个适用于 Android 和 iOS 的应用程序,只要服务器运行,它就会每 5 秒向服务器发送一个新的地理位置。然而,在 iOS 上,应用程序会在随机间隔后停止发送这些位置。虽然我自己并不完全相信这是由于应用程序在 iOS 中暂停(因为它随机停止而不是在固定时间),但我仍然渴望尝试并确定。

但是;我真的不知道该怎么做。我在 eventListener 中创建了一个后台服务来查看发生了什么,它立即开始记录(我现在已经在其中放入了控制台日志)。尽管如此,我的地理位置仍然正常。

现在,有人可以给我一些关于如何解决这个问题的指示吗?我是否想停止正常的地理定位监听器并让 BG 服务接管?或者 BGservice 是否让我的正常代码中的地理位置事件监听器现在保持事件状态?

此时我不敢说我​​非常渴望获得任何帮助,哈哈!

这是我现在的地理位置处理以及 BGservice:

//Start button. listens to an eventHandler in location.js
btnStart.addEventListener('click', function (e) {
    if( Titanium.Geolocation.locationServicesEnabled === false ) {
       GPSSaved.text = 'Your device has GPS turned off. Please turn it on.';
    } else {
        if (Titanium.Network.online) {
            GPSSaved.text = "GPS zoeken...";

            //Empty interval and text to make a clean (re)start
            clearInterval(interval);

            //Set a half second timer on the stop button appearing(So people can't double tap the buttons)
            stopTimeout = setTimeout(showStopButton, 1000);

            //Switch the textlabels and buttons from startview to stopview
            stopText.show();
            startText.hide();
            btnStart.hide();

            //Locationhandler
            location.start({ 
                action: function (e) {
                    //Ti.API.info(e.coords.longitude);
                    if (e.coords) {

                        //If the newly acquired location is not the same as the last acquired it is allowed
                        if (e.coords.longitude != lastLon && e.coords.latitude != lastLat) {

                            //set the last acquired locations+other info to their variables so they can be checked(and used)
                            lastLat = e.coords.latitude;
                            lastLon = e.coords.longitude;


                            lastKnownAltitude = e.coords.altitude;
                            lastKnownHeading = e.coords.heading;
                            lastKnownSpeed = e.coords.speed;

                            if (lastLat != 0 && lastLon != 0) {
                                setGPSholder(lastLat, lastLon, lastKnownAltitude, lastKnownHeading, lastKnownSpeed);
                            } else {
                                GPSSaved.text = 'Geen coordinaten.';
                            }
                        }
                    }                   
                }
            });

            /*
            A second interval which shows a counter to the user and makes sure a location is sent
            roughly every 5 seconds(setInterval isn't accurate though)
            A lot of counters are tracked for several reasons:
            minuteInterval:     Counter which makes sure the last location is sent after a minute if no new one is found in the meantime
            secondsLastSent:    The visual counter showing the user how long its been for the last save(Is reset to 0 after a succesful save)
            */
            interval = setInterval(function () {
                minuteInterval++;
                minuteIntervalTest++;
                secondsLastSent++;

                counterBlock.text = "De laatste locatie is " + secondsLastSent + " seconden geleden verstuurd";

                //If the counter is higher than 5 send a new coordinate. If at the same time the minuteInterval is over a minute
                //The last location is put in the array before calling the sendCoordinates
                if (counter >= 5) {
                    if (minuteInterval > 60) {
                        if (lastLat != 0 && lastLon != 0) {
                            setGPSholder(lastLat, lastLon, lastKnownAltitude, lastKnownHeading, lastKnownSpeed);
                        }
                    }
                    counter = 0;
                    sendCoordinates();
                    Ti.API.info(1);
                } else {
                    counter++;
                }

                if (minuteIntervalTest > 60) {
                    sendTestRequest();
                }
            }, 1000);


            if (Titanium.Platform.osname == 'iphone' || Titanium.Platform.osname == 'ipad') {
                //var service = Ti.App.iOS.registerBackgroundService({url:'send_geolocation_service.js'});
                var service;

                // Ti.App.iOS.addEventListener('notification',function(e){
                // You can use this event to pick up the info of the noticiation. 
                // Also to collect the 'userInfo' property data if any was set
                //      Ti.API.info("local notification received: "+JSON.stringify(e));
                //  });
                // fired when an app resumes from suspension
                Ti.App.addEventListener('resume',function(e){
                    Ti.API.info("app is resuming from the background");
                });
                Ti.App.addEventListener('resumed',function(e){
                    Ti.API.info("app has resumed from the background");
                    // this will unregister the service if the user just opened the app
                    // is: not via the notification 'OK' button..
                    if(service!=null){
                        service.stop();
                        service.unregister();
                    }
                            Titanium.UI.iPhone.appBadge = null;
                });
                Ti.App.addEventListener('pause',function(e){
                    Ti.API.info("app was paused from the foreground");

                    service = Ti.App.iOS.registerBackgroundService({url:'send_geolocation_service.js'});
                    Ti.API.info("registered background service = "+service);

                });
            }



        } else {
            stopGPS();
            GPSSaved.text = "Geen internetverbinding.";
        }
    }
});

正如您所看到的,有一些计数器按一定时间间隔运行,以决定是否应每 5 秒或每分钟发送一次地理位置(如果自上次发现以来没有新位置)

tl;dr: 我希望每 5 秒发送一次地理位置信息,但不知何故,iOS(iPhone 4s 和 5 已测试)在随机时间段后停止发送,并在我取出手机后重新开始发送待机。

最佳答案

实际上后台服务有一个限制,即在 10 分钟后停止,因此如果您想在设备处于后台模式时捕获位置,则需要在 tiapp.xml 文件中设置模式标记。

只需引用此在线文档即可了解其工作原理。 http://docs.appcelerator.com/titanium/3.0/#!/guide/tiapp.xml_and_timodule.xml_Reference-section-29004921_tiapp.xmlandtimodule.xmlReference-LegacyiPhonesection

关于javascript - 在后台服务中发送地理定位[钛],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16230826/

相关文章:

javascript - 简写如果?返回 : null

javascript - querySelectorAll 选择子代但不选择孙代 Vanilla Javascript

ios - 如何在 Objective-C 中获取 NSArray 中的当前位置

ios - Safari 上 createScriptProcessor 的替代方案?

ios - 使用 swift 以编程方式在当前上下文中以模态方式呈现 View Controller

javascript - 使用 javascript 进行 html 表单验证

javascript - 使用 gulp 创建 .js、.min.js 和 .js.map 文件?

android - HTML5 移动应用程序是否在服务器上运行并通过各种网络浏览器访问?

ios - Titanium - 像Groupon一样的滑动菜单

ios - 钛iOS : tab groups images not showing