关闭/打开位置服务,Android后,地理定位停止工作

时间:2015-09-11 13:04:35

标签: android actionscript-3 geolocation

我有一个大问题,我无法解决它。我有一个用Flash Develop编写的应用程序,用于在工作期间打入和关闭员工并跟踪他们的位置,并在他们进入/退出时跟踪他们的位置。当位置服务一直处于开启状态时,一切正常。但是,如果员工想要关闭位置服务(例如隐藏他的位置),当打开它时,应用程序就无法正常工作 - 它总是告诉我们位置我们静音(关闭)。退出申请并重新开始后,它恢复正常。

有什么建议吗?

我正在使用的代码:

我需要自定义事件来触发位置事件...

package MyEvents {
    import flash.events.Event;
    public class GeoLocUpdateEvent extends Event {
        public static const GEOLOC_UPDATED:String = "GEOLOC_UPDATED";
        public static const GEOLOC_NOTSUPPORTED:String = "GEOLOC_NOTSUPPORTED";
        public static const GEOLOC_MUTED:String = "GEOLOC_MUTED";
        public var latitude:Number;
        public var longitude:Number;
        public var precision:Number;
        public function GeoLocUpdateEvent(type:String) { super(type); }
    }
}

在被问到时实际捕获地理位置的类:

package Utils.Geoloc {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.GeolocationEvent;
    import flash.sensors.Geolocation;
    import MyEvents.GeoLocUpdateEvent;

    public class getGeoloc extends Sprite {
        private var counter:int;
        private var _geo:Geolocation;
        public function getGeoloc() {
            this.addEventListener(Event.REMOVED_FROM_STAGE, deleteListeners);
        }
        public function getloc():void {
            counter=1;
            _geo = new Geolocation();
            if (Geolocation.isSupported) {
                if (!_geo.muted) {
                    _geo.addEventListener(GeolocationEvent.UPDATE, geolocationUpdateHandler);
                    _geo.setRequestedUpdateInterval(1500);
                } else {
                    var custEvent:GeoLocUpdateEvent = new GeoLocUpdateEvent(GeoLocUpdateEvent.GEOLOC_MUTED);
                    this.dispatchEvent(custEvent);
                    _geo = null;
                }
            } else {
                custEvent = new GeoLocUpdateEvent(GeoLocUpdateEvent.GEOLOC_NOTSUPPORTED);
                this.dispatchEvent(custEvent);
                _geo = null;
            }
        }
        private function geolocationUpdateHandler(ev:GeolocationEvent):void {
            counter--;
            if (counter<0) {
                if (ev.horizontalAccuracy < 40) {
                    var custEvent:GeoLocUpdateEvent = new GeoLocUpdateEvent(GeoLocUpdateEvent.GEOLOC_UPDATED);
                    custEvent.latitude = ev.latitude;
                    custEvent.longitude = ev.longitude;
                    custEvent.precision = ev.horizontalAccuracy;
                    this.dispatchEvent(custEvent);
                    _geo.removeEventListener(GeolocationEvent.UPDATE, geolocationUpdateHandler);
                    _geo = null;
                }
            }
        }
        private function deleteListeners(ev:Event):void {
            _geo = null;
        }
    }
}

一直在运行的类(当员工工作时,具有时钟输入/时钟输出功能。它每隔5分钟检查一次位置并将其存储到数据库中。

package Utils.Geoloc {
    import flash.display.Sprite;
    import flash.events.TimerEvent;
    import flash.utils.Timer;
    import MyEvents.GeoLocUpdateEvent;

    public class GeoLocWhileWorking extends Sprite {
        private var _geo:getGeoloc;
        private var Tim:Timer;
        private var intervalToRun:Number = 300000;

        public function GeoLocWhileWorking() {

        }

        /********** while working **********/
        private function check(e:TimerEvent):void {
            if (Globals.atWork) {   //global indicates employee has clocked-in
                _geo = new getGeoloc();
                _geo.addEventListener(GeoLocUpdateEvent.GEOLOC_UPDATED, geolocationUpdateHandler);
                _geo.addEventListener(GeoLocUpdateEvent.GEOLOC_MUTED, geolocationUpdateHandlerMuted);
                _geo.getloc();
                Tim.stop();
                Tim.removeEventListener(TimerEvent.TIMER, check);
                Tim = null;
            }
        }
        private function geolocationUpdateHandler(ev:GeoLocUpdateEvent):void {
            Globals.mainStage.SQL.locationrecord(0, 0, ev.latitude, ev.longitude, ev.precision);
            continueTimer();
        }
        private function geolocationUpdateHandlerMuted(ev:GeoLocUpdateEvent):void {
            Globals.mainStage.SQL.locationrecord(0,0,0,0,0);  //stores zero values if location is turned off while working
            continueTimer();
        }
        private function continueTimer():void {
            _geo.removeEventListener(GeoLocUpdateEvent.GEOLOC_UPDATED, geolocationUpdateHandler);
            _geo.removeEventListener(GeoLocUpdateEvent.GEOLOC_MUTED, geolocationUpdateHandlerMuted);
            _geo = null;
            Tim = new Timer(intervalToRun);
            Tim.addEventListener(TimerEvent.TIMER, check);
            Tim.start();
        }

        /********** start/stop working **********/
        public function startworking():void {
            Globals.isEmployeeStarting = 1;
            Globals.isEmployeeEnding = 0;
            _geo = new getGeoloc();
            _geo.addEventListener(GeoLocUpdateEvent.GEOLOC_UPDATED, h1Updated);
            _geo.addEventListener(GeoLocUpdateEvent.GEOLOC_MUTED, hMuted);
            _geo.addEventListener(GeoLocUpdateEvent.GEOLOC_NOTSUPPORTED, hNotSupported);
            _geo.getloc();
        }
        public function stopworking():void {
            Globals.isEmployeeStarting = 0;
            Globals.isEmployeeEnding = 1;
            if (Tim) {
                if (Tim.running) {
                    Tim.stop();
                    Tim.removeEventListener(TimerEvent.TIMER, check);
                    Tim = null;
                }
            }
            _geo = new getGeoloc();
            _geo.addEventListener(GeoLocUpdateEvent.GEOLOC_UPDATED, h1Updated);
            _geo.addEventListener(GeoLocUpdateEvent.GEOLOC_MUTED, hMuted);
            _geo.addEventListener(GeoLocUpdateEvent.GEOLOC_NOTSUPPORTED, hNotSupported);
            _geo.getloc();
        }
        private function h1Updated(ev:GeoLocUpdateEvent):void {
            _geo.removeEventListener(GeoLocUpdateEvent.GEOLOC_UPDATED, h1Updated);
            _geo.removeEventListener(GeoLocUpdateEvent.GEOLOC_MUTED, hMuted);
            _geo.removeEventListener(GeoLocUpdateEvent.GEOLOC_NOTSUPPORTED, hNotSupported);
            _geo = null;
            if (Globals.isEmployeeStarting == 1) {
                Tim = new Timer(intervalToRun);
                Tim.addEventListener(TimerEvent.TIMER, check);
                Tim.start();

                Globals.mainStage.SQL.locationrecord(1, 0, ev.latitude, ev.longitude, ev.precision);
                Globals.isEmployeeStarting = 0;
                Globals.isEmployeeEnding = 0;
                Globals.atWork = true;
                //... other stuff related to start working
            } else if (Globals.isEmployeeEnding == 1) {
                Globals.mainStage.SQL.locationrecord(0, 1, ev.latitude, ev.longitude, ev.precision);
                Globals.isEmployeeStarting = 0;
                Globals.isEmployeeEnding = 0;
                Globals.atWork = false;
                //... other stuff related to start working
            }
        }
        private function hNotSupported(ev:GeoLocUpdateEvent):void {
            _geo.removeEventListener(GeoLocUpdateEvent.GEOLOC_UPDATED, h1Updated);
            _geo.removeEventListener(GeoLocUpdateEvent.GEOLOC_MUTED, hMuted);
            _geo.removeEventListener(GeoLocUpdateEvent.GEOLOC_NOTSUPPORTED, hNotSupported);
            _geo = null;
            var custEvent:GeoLocUpdateEvent = new GeoLocUpdateEvent(GeoLocUpdateEvent.GEOLOC_NOTSUPPORTED);
            this.dispatchEvent(custEvent);
        }
        private function hMuted(ev:GeoLocUpdateEvent):void {
            _geo.removeEventListener(GeoLocUpdateEvent.GEOLOC_UPDATED, h1Updated);
            _geo.removeEventListener(GeoLocUpdateEvent.GEOLOC_MUTED, hMuted);
            _geo.removeEventListener(GeoLocUpdateEvent.GEOLOC_NOTSUPPORTED, hNotSupported);
            _geo = null;
            var custEvent:GeoLocUpdateEvent = new GeoLocUpdateEvent(GeoLocUpdateEvent.GEOLOC_MUTED);
            this.dispatchEvent(custEvent);
        }
    }
}

关闭位置服务后,即使打开了locatgion服务,地理位置对象也会永久静音。我总是收到零作为答案,即使他们无法退房,因为“位置服务已关闭”......

0 个答案:

没有答案