-4

I need for my app an service that tracks the phones location all the time. Its used to create a path that the user traveled, will be displayed in the app and should be distinguishable between walking and other means of movement. The location should be of high accuracy combined with timestamps and speed and saved in an sql lite db for further use in my app.

What is the best way to tackle this problem? I encountered that background services weren't able to get the location often enough. My last resort would be to make it a foreground service but maybe someone has already a solution to this problem. (e.g. is it possible to get the location data google keeps track of in a usable format?) Thanks in advance!

Edit: I am sorry that i wsant clear enough. The problem is that during my research i found out that there were several ways to get the data needed. The location manager and fused locoation. All of these are limited by how often you are allowed to gain GPS data when the service runs in background, which might reduce the path accuracy. Some solutens were to make it a foreground service so it wont suffer from the same limitations, but that isnt something i want to implement.

To formulate a more precise question: What is the best way to collect accurate(!) GPS location data over a long time using a background service while not draining too much energy? It could also be in batches, as it isnt used in real time but to evaluate later.

headput
  • 1
  • 1

1 Answers1

1

Try to just ask specific questions. Otherwise, no one knows how to help or answer you. Since your new on this platform, I'll try my best to answer your question.

First of all. The "data google keeps track of" are from type Location. You can request location updates by using the LocationManager.

Just register for location updates inside your preferred class:

    # Get Instance of LocationManager
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    # Create Criteria / What Accuracy of do you need? No Requirement means also very week locations will be used
    Criteria gpsCriteria = new Criteria();
    gpsCriteria.setAccuracy(Criteria.NO_REQUIREMENT);
    gpsCriteria.setAltitudeRequired(false);
    gpsCriteria.setBearingAccuracy(Criteria.NO_REQUIREMENT);
    gpsCriteria.setCostAllowed(false);
    gpsCriteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
    // Get the wanted provider
    String gpsProvider = locationManager.getBestProvider(gpsCriteria, true);

    // Request for Location Updates your Class must implement a LocationListener Every second a location is received
    locationManager.requestLocationUpdates(gpsProvider, 1000, 0, this);
    Location location = locationManager.getLastKnownLocation(gpsProvider);

In your class, you implement the LocationListener as mentioned above. In onLocationChange, you get Location Updates of the device. I don't know how you want to display the track of the user. But you can get the Latitude and Longitude from this location object. You have also indicators like quality etc.

@Override
public void onLocationChanged(Location location) {
     location.getLatitude() ...
}

If you want to track the Location all the time you have to implement a BackgroundService. Your request for location updates in onStartCommand. However, you have to handle DeepSleep and other functions that set the device into sleep. But you can handle this with a WakefulBroadcastReciever and AlarmManager. Yours create an Alarm which sends an Intent to which is received by your receiver, the receiver restarts the service if the service was set Offline. But handle this with care because it drains the battery of the device and I don't recommend it!! Only for some special business usages.

Bilaal Rashid
  • 828
  • 2
  • 13
  • 21
Erythrozyt
  • 802
  • 6
  • 21