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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:12:07+00:00 2026-06-14T11:12:07+00:00

I Need to stop the location listener updates. I know that in order to

  • 0

I Need to stop the location listener updates. I know that in order to stop a location listener I need to call locationManager.removeUpdates(LocationListener) but I keep getting an error of MyLocationListener cannot be resolved to a variable

Here is the code I have:

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class GetCoords extends Activity {

    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; //in Meters
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 10000; //in Milliseconds

    protected LocationManager locationManager;

    protected Button getLocationButton;
    protected Button stopLocationButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_coords);

        getLocationButton = (Button) findViewById(R.id.get_location_button);
        stopLocationButton = (Button) findViewById(R.id.stop_location_button);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,
                MINIMUM_TIME_BETWEEN_UPDATES,
                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                new MyLocationListener()
        );

        getLocationButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showCurrentLocation();
            //String message = "GPS Service Started";
            Toast.makeText(GetCoords.this, "GPS Service Started", Toast.LENGTH_SHORT).show();
        }
    });
    stopLocationButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            stopMyLocation();
            //locationManager = null;
            //stopCurrentLocation();
            Toast.makeText(GetCoords.this, "GPS Service Stopped", Toast.LENGTH_SHORT).show();
        }
    });
        }

protected void stopMyLocation() {
    locationManager.removeUpdates(MyLocationListener);
            locationManager = null;
}

I don’t understand what I am doing wrong, every example I have seen of how to stop GPS updates makes me think that my code under stopMyLocation() is correct. I have tried it inside the stopLocationButton click listener, in its current location, pretty much everywhere and it keeps telling me that MyLocationListener is incorrect.

  • 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-14T11:12:08+00:00Added an answer on June 14, 2026 at 11:12 am

    Save a reference to your LocationListener:

    protected LocationManager locationManager;
    protected MyLocationListener myLocationListener = new MyLocationListener();
    

    Change this:

        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,
                MINIMUM_TIME_BETWEEN_UPDATES,
                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                myLocationListener
        );
    

    Now you can cancel it with:

    locationManager.removeUpdates(myLocationListener);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When writing server-side code you need to explicitly stop execution after sending a Location:
I'm running a long simulation in MATLAB that I've realized I need to stop
My app registers for background location updates (not Significant Change. I need more granularity
I have an app that uses the CLLocationManager to receive location and heading updates.
We need to stop copy paste of few characters like '<' or '>' or
I need to stop the default operation of window being closed when red x
I need to stop a pair of services just before I delete a database.
I am receiving shouldstartloadwithrequest event and I need to stop request for some conditions.
When developing for Android do you typically need to stop the emulator and restart
I am using the SmartWizard 2.0 ( link ) and I need to stop

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.