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

  • Home
  • SEARCH
  • 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 8974531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:42:04+00:00 2026-06-15T18:42:04+00:00

i have a LocationService class (extends Service implements LocationListener ) that works constantly updates

  • 0

i have a LocationService class (extends Service implements LocationListener) that works constantly updates a global variable whenever a new location is obtained via the onLocationChanged method.

in the onCreate method of my launcher activity, i start this service. the next thing i do is to call a web service that takes in the latitude and longitude from the global variable.

this gives me a NullPointerException on the location global variable and i am assuming its because the service is running on a separate thread and have not obtained a location yet.

Question: how do i make it such that i will only call the web service only when a location has been obtained from the service class?

thanks so much. 🙂

  • 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-15T18:42:05+00:00Added an answer on June 15, 2026 at 6:42 pm

    YourService informs every registered LocationOperator when location is changed. LocationOperator is a simple interface, declares operate(Location) method.

    class YourService extends Service implements LocationListener {
        private List<LocationOperator> listeners = new ArrayList<LocationOperator>();
    
        public List<LocationOperator> getListeners() {
                return listeners;
        }
    
        @Override
        public void onLocationChanged(Location location) {
                for (LocationOperator listener : listeners) {
                        listener.operate(location);
                }
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
                // request location updates from `LOCATION_SERVICE`
                return Service.START_NOT_STICKY;
        }
    
        //...
    }
    

    Start and connect to this service from your main activity

    public class MainActivity extends Activity {
    
        static YourService yourService;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            //...
            Intent service = new Intent(this, YourService.class);
            startService(service);
            bindService();
        }
    
        // ...
    }
    

    Then you can reach this service over MainActivity and register for Location

    public class OperatorActivity extends Activity implements LocationOperator {
    
        @Override
        public void onCreate(Bundle instance) {
            // ...
            MainActivity.yourService.getListeners().add(this);
            // ...
        }
    
        public void operate(Location location) {
            // use location
        }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that extends Fragment and implements LocationListener. When I write LocationManager
Inside public class IAmHere extends Activity implements LocationListener { i have @Override public void
I have a location listener class and a service class. In my location listener
In my app, I have a service that gets the location of the device
I have this simple service that broadcasts the current location of the user. I
I have the following piece of code: public class DumpLocationLog extends Thread { LocationManager
I have a issue where I'm trying to create a class that uses the
Somewhat new to android, need some help with services. I have a service which
Hi I have a class that basically gathers GPS data (Longitude, Lattitude, and Bearing),
i have try with commonsware sample here is the activity public class ConstantsBrowser extends

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.