Tracking Location when app closed

Hi everyone
I am not a dev but working with..

Could you please tell if there s a way to track location even when an app is closed ? For Android & IOS
The main idea is to track location change and send data to server
It seems that it is something that already may exist .. am i wrong ?
could you provide us with some insights on the best way to do this
Many many thanks

Regards

Posts

  • RyanFrenchRyanFrench USMember ✭✭
    edited October 2018

    You will not be able to find a library that does location tracking for both IOS and Android as each implementation will need to rely heavily on its platforms native features.

    Android:

    First you will need a ForegroundService that you start in Main like this:

    StartForegroundService(new Intent(this, typeof(YourAppsService)));

    Next you will need to make a request to ignore battery optimization:

    Intent intent = new Intent();
                String packageName = Android.App.Application.Context.PackageName;
                PowerManager pm = (PowerManager)Android.App.Application.Context.GetSystemService(Context.PowerService);
    
                if (!pm.IsIgnoringBatteryOptimizations(packageName))
                {
                    intent.SetAction(Android.Provider.Settings.ActionRequestIgnoreBatteryOptimizations);
                    intent.SetData(Android.Net.Uri.Parse("package:" + packageName));
                    this.StartActivity(intent);
                }
    

    You should also request a wake lock, there are examples on how to do this.

    This service should make use of a broadcastReciever to monitor when Location is toggled off and on. When location is on it will start requesting location updates using Android's LocationManager.

    To start tracking location use the following:

                _locMngr.RemoveUpdates(this);
                if (_handlerThread != null) //ensure there is only one thread running
                {
                    _handlerThread.Quit();
                    _handlerThread = null;
                }
    
                _handlerThread = new HandlerThread("LocationLoggerThread", (Int32)ThreadPriority.Background);
                _handlerThread.Start();
                _locMngr.RequestLocationUpdates("gps", 60000, 0, this, _handlerThread.Looper);
    
  • arnaudtriperzarnaudtriperz Member ✭✭

    Thank you for your answer
    I realize I ve made a mistake in my description
    Our app already have foreground service allowing us to track location continuously even when I swipe the app

    What we re willing to do is, once a user has registered, no need to launch app to send his location if
    1/he s moving above 200m -> start a service able to send this position to server and this for every 200m move
    2/if he stops moving, send the last location to server every 2mn
    3/if he stop moving for 1h, stop sending his location.

    I am really sorry for my first description I realize it was not the point

    Thank for the help

  • what are you doing to solve your problem ?

  • arnaudtriperzarnaudtriperz Member ✭✭

    nothing at this time because we had to focus on another priority... but if you look to other apps on the market, it looks like sending position to a server even while an app is closed and killed is something that can be achieved

Sign In or Register to comment.