title: Track user movement and update Polyline overlay using an android background service?
body: <p>I am working on creating a fitness tracking app. At the moment, my app will track the users movement and plot a poly line overlay on the google maps fragment exactly how I want it to. The only issue is that it only does this when the app is open (i.e not running in the background or when the phone is locked). I've gathered that the way that I will want to combat this is to use a service to continue to track my location in the background. I'm very new to android app development and have a few questions. Will I be able to update the polyline in the background so that when the user reopens the app the route of their workout will be up to date? How exactly would I go about turning my current android activity that tracks the users movement into a service that tracks it even when the phone is locked? Below is the activity that tracks all of the users movement. </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using Android.App;
  using Android.Content;
  using Android.Gms.Maps;
  using Android.Gms.Maps.Model;
  using Android.Locations;
  using Android.OS;
  using Android.Runtime;
  using Android.Views;
  using Android.Widget;
  using Plugin.CurrentActivity;
  using Plugin.Geolocator;
  using Plugin.Geolocator.Abstractions;
  
  namespace TriTrack
  {
      [Activity(Label = "MapsActivity")]
      public class MapsActivity : Activity, IOnMapReadyCallback
      {
          GoogleMap daMap;
          Position position;
          IGeolocator locator = CrossGeolocator.Current;
          double lat;
          double _long;
          TextView latlonglist;
          PolylineOptions polyline = new PolylineOptions().InvokeWidth(20).InvokeColor(Color.Red.ToArgb());
  
          MarkerOptions start = new MarkerOptions();
          bool WorkoutInProgress = false;
  
          protected override void OnCreate(Bundle savedInstanceState)
          {
              CrossCurrentActivity.Current.Activity = this;
              base.OnCreate(savedInstanceState);
              SetContentView(Resource.Layout.Map);
              MapFragment mapFragment = (MapFragment)FragmentManager.FindFragmentById(Resource.Id.the_fucking_map);
              mapFragment.GetMapAsync(this);
              Button switchB = FindViewById&lt;Button&gt;(Resource.Id.switch_button);
              latlonglist = FindViewById&lt;TextView&gt;(Resource.Id.LATLONG);
              getPos();
  
              switchB.Click += delegate
              {
                  if(WorkoutInProgress == false){
                      WorkoutInProgress = true;
                      start.SetPosition(new LatLng(position.Latitude, position.Longitude));
                      start.SetTitle("Start");
                      daMap.AddMarker(start);
                      polyline.Add(new LatLng(position.Latitude, position.Longitude));
                      StartListening();
                      LatLng latlng = new LatLng(position.Latitude, position.Longitude);  
                      CameraUpdate camera = CameraUpdateFactory.NewLatLngZoom(latlng, 15);  
                      daMap.MoveCamera(camera);
                      switchB.Text = "WORKOUT IN PROGRESS";
                  }
                  else{
                      WorkoutInProgress = false;
                      StopListening();
                      Android.App.AlertDialog.Builder diaglog = new AlertDialog.Builder(this);
                      AlertDialog alert = diaglog.Create();
                      alert.SetTitle("Good Work");
                      alert.SetMessage("Your workout is complete, would you like to record it?");
                      alert.SetButton("Yes", (c, ev) =&gt; {
                          alert.Dismiss(); //TODO: save polyine data to new table in the database.
                      });
                      alert.SetButton2("No", (c, ev) =&gt;{
                          alert.Dismiss(); 
                      });
                      alert.Show();
  
                  }
  
  
  
              };
  
          }
  
          void Locator_PositionChanged(object sender, PositionEventArgs e)
          {
              position = e.Position;
              string latstring = position.Latitude.ToString();
              string longstring = position.Latitude.ToString();
              latlonglist.Text += (latstring + "," + longstring + "\n");
              DrawMarker();
              //FindViewById&lt;Button&gt;(Resource.Id.switch_button).Text = position.Latitude.ToString();
          }
  
  
          public void DrawMarker(){
              lat = position.Latitude;
              _long = position.Longitude;
              polyline.Add(new LatLng(lat, _long));
              //polyline.Add(new LatLng(40.739487, -96.65715119999999)); THIS IS FOR DEBUGGING
              daMap.AddPolyline(polyline);
          }
  
  
          public void OnMapReady(GoogleMap googleMap)
          { 
              daMap = googleMap;
              daMap.MapType = GoogleMap.MapTypeHybrid;
              getPos();
          }
          public async void getPos(){
              position = await locator.GetPositionAsync();
  
          }
          public async void StartListening(){
              await locator.StartListeningAsync(new TimeSpan(0, 0, 0, 3), 0.0, true, new Plugin.Geolocator.Abstractions.ListenerSettings
              {
                  ActivityType = Plugin.Geolocator.Abstractions.ActivityType.Fitness,
                  AllowBackgroundUpdates = true
              });
              locator.PositionChanged += Locator_PositionChanged;
          }
  
          public async void StopListening(){
              await locator.StopListeningAsync();
          }
  
      }
  }
  </code></pre>