1

I am looking to find information about checking for a location on android (approximately every 15 minutes) throughout the day and storing the geo-coordinates. This will happen when the app is in the background also, not just when the app is running in the foreground. I am not sure on the search terms and I can't seem to find anything. Can anyone even suggest search terms to point me in the right direction?

Many Thanks, Kevin

EDIT:

public class MyService extends Service
{
private static final String TAG = "Location";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1000;
private static final float LOCATION_DISTANCE = 10f;

private class LocationListener implements android.location.LocationListener{
Location mLastLocation;
public LocationListener(String provider)
{
    Log.i(TAG, "LocationListener " + provider);
    mLastLocation = new Location(provider);
}
public void onLocationChanged(Location location)
{
    Log.i(TAG, "onLocationChanged: " + location);
    mLastLocation.set(location);
}
public void onProviderDisabled(String provider)
{
    Log.i(TAG, "onProviderDisabled: " + provider);            
}

public void onProviderEnabled(String provider)
{
    Log.i(TAG, "onProviderEnabled: " + provider);
}

public void onStatusChanged(String provider, int status, Bundle extras)
{
    Log.i(TAG, "onStatusChanged: " + provider);
}
} 
LocationListener[] mLocationListeners = new LocationListener[] {
    new LocationListener(LocationManager.GPS_PROVIDER),
};
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.i(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);       
return START_STICKY;
}
@Override
public void onCreate()
{
Log.i(TAG, "onCreate");
initializeLocationManager();
try {
    Thread LocThread = new Thread(new Runnable() {
        public void run() {
            mLocationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, LOCATION_INTERVAL,     LOCATION_DISTANCE,
                    mLocationListeners[0]);
           }
        });

        try {
            LocThread.join();
        } catch (InterruptedException e) {
            Log.i("LocationError","Thread could not join");
            e.printStackTrace();
        }

} catch (java.lang.SecurityException ex) {
    Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
    Log.i(TAG, "gps provider does not exist " + ex.getMessage());
}
}
@Override
public void onDestroy()
{
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
    for (int i = 0; i < mLocationListeners.length; i++) {
        try {
            mLocationManager.removeUpdates(mLocationListeners[i]);
        } catch (Exception ex) {
            Log.i(TAG, "fail to remove location listners, ignore", ex);
        }
    }
}
} 
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null) {
    Thread LocThread = new Thread(new Runnable() {
        public void run() {
            mLocationManager = (LocationManager)     getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
           }
        });

        try {
            LocThread.join();
        } catch (InterruptedException e) {
            Log.i("LocationError","Thread could not join");
            e.printStackTrace();
        }

}
}
}
Kevin
  • 51
  • 2
  • 9

1 Answers1

0

You should read about how to get Location Information in android and How to schedule a service periodically using AlarmManager.

Community
  • 1
  • 1
Manish
  • 3,913
  • 2
  • 29
  • 45
  • Thank you. I have found out some more information about this and I have tried implementing something. I can get the AlarmManager to call the onStartCommand periodically but can't get the location service working. I will post my code in the original question - I would be grateful if anyone could help. Thanks. – Kevin Aug 28 '13 at 11:32