MonoTouch - Location services in background

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

MonoTouch - Location services in background

mobiledev1600
I have an app using MonoTouch which requires to capture GPS data every 2 mins in background. I found two ways to do it

using startMonitoringSignificantLocationChanges- works in background but not every 2 mins.
Using Timer and StartUpdatingLocation - Timer will be called every 2 mins and then update location.
The problem with startMonitoringSignificantLocationChanges is it doesn't get fired every 2 mins. So i cant use it. The other option of using Timer is better since it gives me a handle over time. But, will it run till my app is in the background for example one hour? Usually, ios apps gets 10 mins in background.

I have following code but it doesn't fire timer in background. What is missing


public override void DidEnterBackground(UIApplication application)
                {
                        // App entered background, do some light stuff here,

                        // Location Services
                        // BACKGROUND LOCATION SERVICES
                        if (UIDevice.CurrentDevice.IsMultitaskingSupported && this.taskID == 0)
                        {
                                // START BACKGROUND TIMER
                                backgroundTimer = new System.Timers.Timer(1000);
                               
                                // Hook up the Elapsed event for the timer.
                                backgroundTimer.Elapsed += new ElapsedEventHandler(OnBackgroundTimedGPSEvent);
                                backgroundTimer.Enabled = true;

                                this.taskID = application.BeginBackgroundTask(delegate {

                                        application.EndBackgroundTask(taskID);
                                        this.taskID = 0;
                                });

                                ThreadPool.QueueUserWorkItem(delegate {


                                        backgroundTimer.Start();

                                        application.EndBackgroundTask(this.taskID);
                                        this.taskID = 0;

                                });
                               

                        }

                }

Thanks
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: MonoTouch - Location services in background

Nic Wise
The code isn't going to work, I think.

Basically, you are starting a background task:

this.taskID = application.BeginBackgroundTask(delegate {

and then stopping it right away:

application.EndBackgroundTask(taskID); (in the other thread tho?)

So you app just stops like normal. Try taking the EndBackgroundTask
stuff out and see if it changes it at all.

HOWEVER: with this method, you will run for 6 mins, then iOS will kill
you. You can't, EVER, extend this. (ok, except in iOS7.... maybe)

A better way, if you can classify yourself as a "location based app",
is to use "Required Background Modes".

Using this allows your app to stay running _all the time_, it'll never
be killed. The options are things like audio, voip, location etc (you
DO need to set to the right one for your app - using voip for a
location app will get you kicked off the store, tho if it's a
non-store app you might be fine)

Once you have done that (it's a setting in Info.plist BTW), start the
location manager like normal. If you get a call back, you can just
check

UIApplication.SharedApplication.ApplicationState == UIApplicationState.Active


 and act accordingly (active == I'm in the foreground. Not active ==
I'm in the background, or the phone is locked - dont mess with the
UI!)

(source: http://www.raywenderlich.com/29948/backgrounding-for-ios )

Aside from that, your options are (once you are running all the time):

1. Turn on the location manager. Leave it on. Log things every 2 mins,
and ignore the others. (easy!)
2. Turn on the LM, and get your location. Turn it off. Wait 2 mins.
Turn it on. Repeat. (not as easy, but still not hard)

I suggest you do some testing - there may be a large battery usage
difference between the 2, as it may be more costly to spin up the
location manager than it is to keep one running over a longer period
of time.

Either way, it'll use the usual AGPS stuff, so WIFI, then Cell, then real GPS.

startMonitoringSignificantLocationChanges, Ive found, is unreliable
especially in cities. It's based on a cell site change, usually, and I
think, for me, London has very large cells (with a lot of
micro-transmitters). So my 6km trip from home to work resulted in 0 to
1 "significant" change. which is useless.

You might also want to look into geofencing, which means you can put a
"box" around a point, and if the user enters or leaves it, you get
told. Handy for "sync when I leave home" type things.


On 17 June 2013 21:20, mobiledev1600 <[hidden email]> wrote:

> I have an app using MonoTouch which requires to capture GPS data every 2 mins
> in background. I found two ways to do it
>
> using startMonitoringSignificantLocationChanges- works in background but not
> every 2 mins.
> Using Timer and StartUpdatingLocation - Timer will be called every 2 mins
> and then update location.
> The problem with startMonitoringSignificantLocationChanges is it doesn't get
> fired every 2 mins. So i cant use it. The other option of using Timer is
> better since it gives me a handle over time. But, will it run till my app is
> in the background for example one hour? Usually, ios apps gets 10 mins in
> background.
>
> I have following code but it doesn't fire timer in background. What is
> missing
>
>
> public override void DidEnterBackground(UIApplication application)
>                 {
>                         // App entered background, do some light stuff here,
>
>                         // Location Services
>                         // BACKGROUND LOCATION SERVICES
>                         if (UIDevice.CurrentDevice.IsMultitaskingSupported && this.taskID == 0)
>                         {
>                                 // START BACKGROUND TIMER
>                                 backgroundTimer = new System.Timers.Timer(1000);
>
>                                 // Hook up the Elapsed event for the timer.
>                                 backgroundTimer.Elapsed += new
> ElapsedEventHandler(OnBackgroundTimedGPSEvent);
>                                 backgroundTimer.Enabled = true;
>
>                                 this.taskID = application.BeginBackgroundTask(delegate {
>
>                                         application.EndBackgroundTask(taskID);
>                                         this.taskID = 0;
>                                 });
>
>                                 ThreadPool.QueueUserWorkItem(delegate {
>
>
>                                         backgroundTimer.Start();
>
>                                         application.EndBackgroundTask(this.taskID);
>                                         this.taskID = 0;
>
>                                 });
>
>
>                         }
>
>                 }
>
> Thanks
>
>
>
> --
> View this message in context: http://monotouch.2284126.n4.nabble.com/MonoTouch-Location-services-in-background-tp4658350.html
> Sent from the MonoTouch mailing list archive at Nabble.com.
> _______________________________________________
> MonoTouch mailing list
> [hidden email]
> http://lists.ximian.com/mailman/listinfo/monotouch



--
Nic Wise
t.  +44 7788 592 806 | @fastchicken
b. http://www.fastchicken.co.nz/
_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: MonoTouch - Location services in background

mobiledev1600
Thanks Nic for the details. So the conclusion is

startMonitoringSignificantLocationChanges - very unreliable. We can't set distance filter (give location after every 2km travel) or time filter (give location every 2 mins)

Using Timer & LM - will get killed by iOS after 6 mins. Can't run it more than that.

There is no perfect solution to this. I am wondering how the apps are implementing it. I think lots of app are there out which capture location data in background frequently. Which approach are they using????
Loading...