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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:43:47+00:00 2026-06-04T17:43:47+00:00

So it’s a well known fact that it takes a while ( about 10-12

  • 0

So it’s a well known fact that it takes a while ( about 10-12 seconds ) for the GPS receiver to lock on to the satellites. Using Android LocationListener I want to get notified when the location updates start. For instance:

I have an activity with the following code to instantiate and call the LocationListener

public void startLocationUpdates() {
    try{
    Common.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            listener = new LocListener();
            if(Common.locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                Common.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
            } else if(Common.locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                Common.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

Here’s the LocationListener onLocationChanged implementation.

public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        try{
            Common.currentLocation = new String(String.valueOf(location.getLatitude()) + "," + String.valueOf(location.getLongitude()));
            Log.i("LocListener",Common.currentLocation);
        } catch(Exception e) {
                e.printStackTrace();
        }

    }

The Common.currentLocation string gets updated after the locationUpdates start. However, I want to wait in my main activity until this field is updated.

If I put a while loop in my activity to wait for the Common.currentLocation field to be updated, the thread gets suspended and the onLocationChanged method doesn’t get called ( the Log messages are not printed ).

What’s the best way of getting an update from the onLocationChanged method to a common interface/activity as soon as onLocationChanged gets called and the latitude and longitude values are available ? Any help will be greatly appreciated.

  • 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-06-04T17:43:49+00:00Added an answer on June 4, 2026 at 5:43 pm

    If you need to put the listener in a class for re usability purposes then there a number of methods to provide the information

    The important thing to remember is that Android is an event driven system, so instead of “Waiting” for the results by polling you need to create a way that lets the class create an even and then respond to any interested parties of that event.

    The simplest thing to do if you don’t require re usability is to simply implement the listener in the same activity and do your processing there so for instance

    public class yourclass extends Activity implements LocationListener {
      ...
    
      public void onLocationChanged(Location location) {
         ...
      }
    
    }
    

    The next simplest method is to create an interface in your class with the listener so, this is not error free and from memory but you will get the general idea, in reality this just puts one layer between your class and the event listener but it does isolate you listener into a class that can then easily be used by other activitys, also not I am not doing anything to take into account multiple activity registration de-registration etc.

    public mylistenerclass implements LocationListener {
       ...
    
       public interface LocationEvents { 
          public abstract void onLocationEvent(Location location); 
       }
    
       LocationEvents mListener = null; 
    
       public void registerForEvents(LocationEvents eventListener, ... any-params) {
          ... start listening for location stuff
          mListener = eventListener;
    
       }
    
        public void unregisterForEvents(LocationEvents eventListener) {
          ... end listening for location stuff
          mListener = null;
    
        } 
    
        public void onLocationChanged(Location location) {
             ... 
             if (null != mListener)
                mListener.onLocationEvent(location); 
        } 
    }
    

    public class yourclass extends Activity implements LocationEvents {
    …

      @Override
      public void onResume() {
          super.onResume();      
          registerForEvents(locationEvent, anyparams); 
      }
    
      @Override
      public void onPause() {
          super.onPause();      
          unregisterForEvents(locationEvent); 
      }
    
    }
    

    You could also use a service, or broadcast intents from your class. I suggest though if your other activities don’t need the functionality to just start by implementing the listener in your existing activity and go for there

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build

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.