Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6133781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:16:19+00:00 2026-05-23T17:16:19+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:16:20+00:00Added an answer on May 23, 2026 at 5:16 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In java i have created a JAX- web service, which is up and running,
I have created a WCF service for uploading images , which accepts System.IO.Stream as
I have created a web service which is saving some data into to db.
I have created a web service which takes a username and password as parameters
I have created a web service which returns some data and am trying to
I have created one WCF service, in which I am reading the msn whether
I have created a WCF service that is hosted using windows service. The windows
I have added a background service to my application which creates a notificaion when
I have a service that has its own thread running on background. I'd like
I have a setup where I am running a background service in Android with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.