0

サーバーが実行されている限り、5 秒ごとに新しいジオロケーションをサーバーに送信するチタンを使用して、Android および iOS 用のアプリを作成しています。ただし、iOS では、アプリはランダムな間隔の後にそれらの場所の送信を停止します。私自身は完全には確信していませんが、これはiOSでアプリが一時停止するという事実によるものです(固定時間ではなくランダムに停止するため)、私はまだ試して確信したいと思っています.

でも; 私は本当にこれを行う手がかりがありません。何が起こるかを確認するために、eventListener にバックグラウンド サービスを作成し、すぐにログ記録を開始しました (今のところ、コンソール ログを入れています)。それにもかかわらず、私の地理位置情報も正常にカチカチ音をたてています。

さて、誰かがこれを乗り越える方法についていくつかの指針を教えてもらえますか? 通常のジオロケーション リスナーを停止して、BG サービスに引き継がせますか? または、BGservice は現在、通常のコードで geolocation eventlistener をアクティブに保ちますか?

この時点で、私は何か助けが欲しいと言うのが怖いです、ハハ!

これが、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 秒ごとに送信するか、1 分ごとに送信するかを決定するために、一定の間隔で実行されているカウンターがいくつかあります (最後の場所以降に新しい場所が見つからない場合)。

tl;dr:地理位置情報を 5 秒ごとに送信したいのですが、どうにかして iOS (テスト済みの iPhone 4s および 5) がランダムな時間間隔の後に送信を停止し、電話がスタンバイ状態から抜けた瞬間に送信を再開します。

4

1 に答える 1