Marco Fedele
Marco Fedele

Reputation: 2148

Android Gps saved too much points

I have created a service running in background which saves gps points on the phone db.

I have created the service using also a broadcast listener to the on-off screen, because when the screen is off I want to save points every 5 minutes, when the screen is on I want to save them every minute.

Yesterday I tried the app on my cell phone, and, in 10h it saved more than 12000 points!!!!

After looked deeply I found out that the same point is saved in more copies, from 0 to over 250, and also that the frequency of point saving (withouth the copies) is one every 36 seconds less or more...

Here is the code of the service:

public class MonitorGpsService extends Service {


private String sharedPreferences= "Settings";
private String viaggio_in_corso = "viaggio_in_corso";
private long id;
private boolean firstStart = true;  
private int minTime = 300000; //(5 minuti)
private static String startScan = "dateStart";
private GpsListener gpsListener;
private BroadcastReceiver mReceiver;
private Travel travel;

@Override
public void onCreate() {


    id = this.getSharedPreferences(sharedPreferences, MODE_PRIVATE).getLong(viaggio_in_corso, -1);

    travel = new Travel(this);
    travel.load(id);

    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    mReceiver = new ScreenReceiver();
    registerReceiver(mReceiver, filter);

    gpsListener = new GpsListener(travel);
    gpsListener.startListener(minTime,0);

    this.getSharedPreferences(sharedPreferences, MODE_PRIVATE).edit().putLong(startScan, DateManipulation.getCurrentTimeMs()).commit();
    this.getSharedPreferences(sharedPreferences, MODE_PRIVATE).edit().putBoolean("service", true).commit();
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onDestroy() {
    this.getSharedPreferences(sharedPreferences, MODE_PRIVATE).edit().putBoolean("service", false).commit();
    gpsListener.stopListener();
    unregisterReceiver(mReceiver);  
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    if ((flags & START_FLAG_RETRY) == 0) {
        // TODO If it’s a restart, do something.
    }
    else {
        // TODO Alternative background process.
    }
    boolean screenOn;

    try {   
       screenOn = intent.getBooleanExtra("screen_state", false); 
    } catch (NullPointerException e) { screenOn = false; }

    if ((!screenOn) && (!firstStart)) {

        gpsListener.stopListener();
        gpsListener.startListener(60000, 0);
        this.getSharedPreferences(sharedPreferences, MODE_PRIVATE).edit().putLong(startScan, DateManipulation.getCurrentTimeMs()).commit();
    } 
    else if (!firstStart) {

        gpsListener.startListener(minTime, 0);  
        MediaScan mediaScan = new MediaScan(travel);
        try {
            mediaScan.scanImages();
        } catch (URISyntaxException e) {e.printStackTrace();}

    }
    firstStart = false;
    return Service.START_STICKY;
}

Here is the code of the gpsListener:

public class GpsListener {

private LocationManager locationManager;
private String bestProvider;
private LocationListener myLocationListener = null;
private String serviceString = Context.LOCATION_SERVICE;
private Travel travel;


public GpsListener(Travel travel) {

    locationManager = (LocationManager)travel.getContext().getSystemService(serviceString);
    bestProvider = locationManager.getBestProvider(setCriteria(), true);
    this.travel=travel;

}

public void stopListener() {        
    locationManager.removeUpdates(myLocationListener);
}

public void startListener(int time, int space) {    

            myLocationListener = new LocationListener() {

                Location location1 = null;

                public void onLocationChanged(Location location) {

                if ((location != null) && (location != location1)) {
                    try {
                        savePosition(location);
                        location1 = location;
                    } catch (SQLException e) { e.printStackTrace(); }
                }           
            }
            public void onProviderDisabled(String provider){
                // Update application if provider disabled.
            }
            public void onProviderEnabled(String provider){
                // Update application if provider enabled.

            }
            public void onStatusChanged(String provider, int status, Bundle extras){
                // Update application if provider hardware status changed.
            }
       };

       locationManager.requestLocationUpdates(bestProvider, time, space, myLocationListener);


    }
    // salva la posizione passatagli nel db
    private void savePosition(Location location) throws SQLException {      

        Points point = new Points();
        point.setLatitude((int) (location.getLatitude() * 1E6));
        point.setLongitude((int) (location.getLongitude() * 1E6));
        point.setDataRilevamento(DateManipulation.getCurrentTimeMs());
        travel.addPoints(point);
    }

    // criteri globali per la gestione del gps
    private Criteria setCriteria() {

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setSpeedRequired(false);
        criteria.setCostAllowed(false);
        return criteria;
    }
}

And here i the code for the ScreenReceiver

public class ScreenReceiver extends BroadcastReceiver {

private boolean screenOff;

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        screenOff = true;
    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
        screenOff = false;
    }
    Intent i = new Intent(context, MonitorGpsService.class);
    i.putExtra("screen_state", screenOff);
    context.startService(i);
    }

}

Thank you for the help.

Upvotes: 2

Views: 281

Answers (1)

sherif
sherif

Reputation: 2332

yeah this is normal I think Ive made an app which saved the position every 100m and 20 seconds the result was I had 5-8 points after the 100m and 20 seconds that was on Android 1.6

just check the distance from the last point and compare it to a min distance variable if its greater then save it

there is a function for distance calculation in the Android api .

EDIT: its the float distanceTo(Location dest) function of a location instance

Upvotes: 2

Related Questions