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 849555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:07:19+00:00 2026-05-15T07:07:19+00:00

Edit: I’m rewriting this question because I apparently wasn’t clear. Sometimes the GPS service

  • 0

Edit: I’m rewriting this question because I apparently wasn’t clear.

Sometimes the GPS service on Android phones takes a long time to get a fix. Sometimes it’s fast, sometimes it takes hours. I know and accept this.

I have an application that does many things. One of the things it must do is allow the user to click a button to send their current coordinates to a server. What I need are the coordinates of the phone when the user clicks the button or within a reasonably short time thereafter.

Because I know that getting a GPS fix is not instant and I know that it could take minutes or hours (during which the user has moved a great distance), I need to code a timeout for this feature. For this feature, it is simply not acceptable to upload the GPS location of the user three minutes (for example) after they clicked the button. It’s fine if it takes 45 seconds, not okay if it takes 75 seconds. It’s fine to give the user an error notification if the feature failed to get a location fast enough.

I need a feature to ‘get the GPS location and send it to the server, unless it takes more than one minute‘.

My original code is below. I have changed some things since posting it. I have added a Timer in the onStartCommand() method. I start a TimerTask that after 60 seconds will call my stop() method. At the beginning of the onLocationChanged() method, I cancel the TimerTask.

My question is: Is the Timer scheme a good way of implementing this timeout? Is there a better way?

Original question:

I’m writing an Android application that, among other things, needs to send the current GPS coordinates to a server when the user tells it to. From a context menu, I run the service below. The service is a LocationListener and requests updates from the LocationManager. When it gets a location (onLocationChanged()), it removes itself as a listener and sends the coordinates off to the server. All of this is working.

However, if GPS coordinates are not quickly available, my service just keeps running until it gets some. It holds up the UI with a progress dialog, which is annoying. Worse, if the user has moved since starting the service, the first GPS coordinates might be wrong and the app will send bad data to the server.

I need a timeout on the service. Is there a good way to do that? I’m not very experienced with threads. I think I can run a Runnable in the onStartCommand() method that will somehow count down 30 seconds and then, if there is no GPS result yet, call my service’s stop() method. Does that sound like the best way to do this?

Alternatively, is it possible to tell if the GPS cannot get a fix? How would I go about doing that?

Edit: To further clarify, I’m looking for the best way to “give up” on getting a Location after some amount of time.

public class AddCurrentLocation extends Service implements LocationListener {

    Application app;
    LocationManager mLocManager;
    ProgressDialog mDialog;

    @Override
    public int onStartCommand(Intent intent, int arg0, int arg1) {
        app = getApplication();

        // show progress dialog
        if (app.getScreen() != null) {
            mDialog = ProgressDialog.show(app.getScreen(), "", "Adding Location. Please wait...", true);
        }

        // find GPS service and start listening
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        mLocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        String bestProvider = mLocManager.getBestProvider(criteria, true);
        mLocManager.requestLocationUpdates(bestProvider, 2000, 0, this);

        return START_NOT_STICKY;
    }

    private void stop() {
        mLocManager.removeUpdates(this);
        if (mDialog != null) {
            mDialog.dismiss();
        }
        stopSelf();
    }

    @Override
    public void onLocationChanged(Location location) {
        // done with GPS stop listening
        mLocManager.removeUpdates(this);

        sendLocation(location); // method to send info to server
        stop();
    }

    // other required methods and sendLocation() ...

}
  • 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-15T07:07:20+00:00Added an answer on May 15, 2026 at 7:07 am

    That’s not really how it works. It will consistently take that long in most situations to get a GPS fix. But from that point on each update (every 2 sec in your code)will be the person’s current position. And the first fix you get will be the person’s current position, so the data will not be “out of date”.

    Another thing. If you are running this code in a service you shouldn’t block the UI with a progress dialog and definitely not from the Service. That is a memory leak waiting to happen. You should only show progress if it is something that might take 5 sec at the most and is running in a thread in the Activity. Another option is to show the progress dialog in the title bar, and still let the user interact with the app (which is why you use a service anyway). Showing progresses for a long period of time really isn’t that User Friendly. Especially if they somehow change orientation (maybe on accident) and then your app crashes because of the service handle to the dialog and they have to start over.

    Take a look at the Google I/O 2010 app to see a great example of how an activity should work with a service. It uses a service to pull back data, and shows a progress in the title while the service is doing some work. And still lets you do other things in the app.

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

Sidebar

Related Questions

EDIT: had to retag this, because, it's rather a Sweave / R question since
Edit: This question was written in 2008, which was like 3 internet ages ago.
EDIT: This question is more about language engineering than C++ itself. I used C++
EDIT: upgraded this question to MVC 2.0 With asp.net MVC 2.0 is there an
Edit: Just wanted to make the question I have more clear. I pretty much
Edit: This is technically a 2 part question. I've chosen the best answer that
EDIT: Please note that this question is OUTDATED; RVM got way easier to use
edit: Retagged as tomcat / jboss , since this could be a question about
EDIT: Problem wasn't related to the question. It was indeed something wrong with my
EDIT: This question was exceptionally dumb and made me look like a script kiddie,

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.