Windows Phone 8 - Geolocator PositionChanged Listener Dies After Some Time

I set the PositionChanged listener in a Geolocator object with

var geolocator = new Geolocator(); geolocator.PositionChanged += Geolocator_PositionChanged; 

It has been working fine for some time. But after a while without user interaction (+ - 4 hours), he stops receiving position changes, I think, because WP8 just kills him. This may be desirable from ms, but it is terrible for my application model.

I did to additionally set PeriodicTask and send the position. And it works without problems, but if the user changes his position, I can not really track it.

Question: is there anyway to awaken this geolocation without the need for user interaction? It can be through PeriodicTask or even in PositionChanged.

I'm already trying to instantiate a new geolocation already inside the PositionChanged delegate, but this does not work.

Any help is greatly appreciated. Thanks!

+6
source share
3 answers

It seems to be some kind of GC that kills / disables the instance.

try using geolocator.addEventListener("statuschanged", onStatusChanged);

and restart the object when the status is Windows.Devices.Geolocation.PositionStatus.disabled or Windows.Devices.Geolocation.PositionStatus.notAvailable .

see Geolocator.StatusChanged | statuschanged event for more details.

I hope I helped

+2
source

Are you specifying a timeout in your call to GetGeopositionAsync ? According to the last comment at the bottom of this post , this code fixes the problem when the GetGeopositionAsync call GetGeopositionAsync not return. It may be related.

 public Task GetCurrentPosition() { return Task.Run(() => { if (_geolocator == null) { _geolocator = new Geolocator { DesiredAccuracy = PositionAccuracy.High }; } Geoposition geoposition = null; ManualResetEvent syncEvent = new ManualResetEvent(initialState: false); IAsyncOperation asyncOp = _geolocator.GetGeopositionAsync(); asyncOp.Completed += (asyncInfo, asyncStatus) => { if (asyncStatus == AsyncStatus.Completed) { geoposition = asyncInfo.GetResults(); } syncEvent.Set(); }; syncEvent.WaitOne(); return geoposition.Coordinate; }); } 
+1
source

It is likely that the garbage collector considers it unused and kills, or at least disables energy saving.

Most of the hardware resources you use have a method

 addEventListener 

This is a way to report this resource: "Hey, I want to receive updates from you, so I am not dying." And this is a very common pattern for observing objects and receiving updates asynchroniusly.

Take a look at http://en.wikipedia.org/wiki/Observer_pattern

+1
source

Source: https://habr.com/ru/post/943716/


All Articles