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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:02:23+00:00 2026-06-01T14:02:23+00:00

I implemented Android service for listening user location: public class ListenLocationService extends Service {

  • 0

I implemented Android service for listening user location:

public class ListenLocationService extends Service {
    IBinder mBinder = new LocalBinder();      
    public interface ILocationService {     
          Location userLocation = new Location("");
          public void StartListenLocation();
          public void StopListenLocation();
          public Location getUserLocation();
        }
    LocationManager locationManager;
    LocationListener locationListener;
    public class LocalBinder extends Binder implements ILocationService{

        public void StopListenLocation(){
            //so many attempts to stop service and no one helped
            locationManager.removeUpdates(locationListener);
            locationListener = null;
            stopSelf();
        }

        public void StartListenLocation()
        {   
            locationManager = (LocationManager)ListenLocationService.this.getSystemService(Context.LOCATION_SERVICE);
            locationListener = new LocationListener() {

                public void onStatusChanged(String provider, int status, Bundle extras) {               
                }       

                public void onProviderEnabled(String provider) {                     
                }

                public void onProviderDisabled(String provider) {                   
                }

                public void onLocationChanged(Location location) {                  
                    userLocation.set(location);
                }
            };
            //if I have only one requestLocationUpdates situation is the same

                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 
                        400, 1, locationListener);
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
                        400, 1, locationListener);
            }

        public Location getUserLocation(){
            return userLocation;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }   

}

I’m binding to this service in two activities. In first activity I’m just launching service:

    private ServiceConnection mConnection = new ServiceConnection() {

        public void onServiceConnected(ComponentName className,
                IBinder service) {
            mService = (ILocationService) service; 
            mService.StartListenLocation();   
        }

        public void onServiceDisconnected(ComponentName arg0) {
            Log.d("LOG","onServiceDisconnected");
        }
    };

    public void onCreate(Bundle savedInstanceState) {
...
        Intent intent = new Intent(this, ListenLocationService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
...
}
    @Override 
    public void onStop(){
        super.onStop();
        unbindService(mConnection);
        mService.StopListenLocation();      
    }

If I launch in my app only first activity and the close it then service stops – GPS mark disappear from device screen.

However if I enter the second activity (where I’m getting userLocation) and after this close both(using Android back button), then GPS mark still on my screen!

Code from second activity:

    private ServiceConnection mConnection = new ServiceConnection() {

        public void onServiceConnected(ComponentName className,
                IBinder service) {
            mService = (ILocationService) service;             
            userLocation = mService.getUserLocation();              
        }

        public void onServiceDisconnected(ComponentName arg0) {
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
...
    Intent intent = new Intent(this, ListenLocationService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
...
}
    @Override 
    public void onStop(){
        super.onStop();
            unbindService(mConnection);
            mService.StopListenLocation();          

    }

You can see that I wrote in both onStop() methods not only unbindService (initially I thought that it’ll be enough) but I’m calling StopListenLocation function with this code:

locationManager.removeUpdates(locationListener);
locationListener = null;
stopSelf();

It stops service if only first activity was bound to it. However if both activities were bound, service doesn’t stop after both onStop(). I used Log.d to get sure that all methods are called: onStop(), stopListenLocation(), onBind() etc. The only thing is that onServiceDisconnected() is not called ever.

I suppose situation is that my service starts another system service for listening location. I stop my service, but this system service that controls GPS continue to work despite of locationManager.removeUpdates(locationListener);. Maybe I’m mistaken in my suggestion.

How can I stop GPS where both activities are stopped?

  • 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-01T14:02:24+00:00Added an answer on June 1, 2026 at 2:02 pm

    BIND_AUTO_CREATE doesn’t necessarily mean that the service will terminate itself when there are no connections, it just means that it’s the first thing on the chopping block if the system needs resources. If the system is not resource starved, it will let your service continue to run, since, if your activity wants the service again, it is less expensive time-wise to bind to an existing instance than to create it from scratch.

    In short, it will keep ‘running’, but don’t worry about it – it’s not consuming anything that is needed elsewhere.

    EDIT: Also note that just because you have unbound from the service does not mean that it will interrupt any running processing tasks that the service is handling asynchronously. All that’s happened is you’ve told the OS that you aren’t interested in the results anymore. You must explicitly stop any running tasks before disconnecting from the service or that task will continue to consume CPU time

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

Sidebar

Related Questions

I have implemented this broadcast reciever: public class ServiceManager extends BroadcastReceiver { private final
I have an android activity and a service implemented using aidl. Works like a
I have implemented an Android code that calls a SOAP web service. The web
I have implemented an HTTPOperations class in Android that can successfully produce an HTTP
I am targeting Android 1.6 The LocationManager and listener are implemented within a Service
I have an Android service that is running and listening for microphone input. I
I've implemented a Service in my android app that starts a timer (using the
I have implemented onRetainNonConfigurationInstance() on one of my Activity to handle android screen orientation.
I have made an android app which working fine. I implemented the Login functionality
i'm a bit stuck with remote services in android. thing is i implemented a

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.