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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:21:53+00:00 2026-05-31T15:21:53+00:00

I have a class which handle a location service called MyLocationManager.java this is the

  • 0

I have a class which handle a location service called MyLocationManager.java

this is the code :

package com.syariati.childzone;

import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class MyLocationManager {

    private Context context;
    private double myLat = 0.0;
    private double myLon = 0.0;
    private LocationManager locationManager = null;
    private Location location = null;
    private Criteria criteria;
    private String locationName = null;

    public MyLocationManager(Context ctx) {
        this.context = ctx;
    }

    private String setCriteria() {
        this.criteria = new Criteria();
        this.criteria.setAccuracy(Criteria.ACCURACY_FINE);
        this.criteria.setAltitudeRequired(false);
        this.criteria.setBearingRequired(false);
        this.criteria.setCostAllowed(true);
        this.criteria.setPowerRequirement(Criteria.POWER_LOW);
        return locationManager.getBestProvider(criteria, true);
    }

    public double getMyLatitude() {
        return this.myLat;
    }

    public double getMyLongitude() {
        return this.myLon;
    }

    public String getLocation() {
        return this.locationName;
    }

    public void onLocationUpdate() {
        locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        String provider = setCriteria();

        location = locationManager.getLastKnownLocation(provider);
        updateWithNewLocation(location);
        locationManager.requestLocationUpdates(provider, 1000, 0,
                new MyLocationListener());

    }

    private void updateWithNewLocation(Location location) {
        if (location != null) {
            this.myLat = location.getLatitude();
            this.myLon = location.getLongitude();
//          Toast.makeText(this.context,
//                  "Lokasi Anda:\n" + this.myLat + "\n" + this.myLon,
//                  Toast.LENGTH_LONG).show();
            getLocationName(this.myLat, this.myLon);
        } else {
            Toast.makeText(this.context, "Lokasi Anda Tidak Diketahui",
                    Toast.LENGTH_SHORT).show();
        }
    }

    private void getLocationName(double lat, double lon) {
        Geocoder geocoder = new Geocoder(context, Locale.getDefault());
        try {
            List<Address> adresses = geocoder.getFromLocation(lat, lon, 1);
            StringBuilder sb = new StringBuilder();
            if (adresses.size() > 0) {
                Address address = adresses.get(0);
                for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
                    sb.append(address.getAddressLine(i)).append("\n");
                sb.append(address.getCountryName()).append("\n");
            }
            this.locationName = sb.toString();
//          Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location newLocation) {
            // TODO Auto-generated method stub
            myLat = newLocation.getLatitude();
            myLon = newLocation.getLongitude();
            getLocationName(myLat, myLon);
            Toast.makeText(context,
                    "Lokasi terupdate\nLat: " + myLat + "\nLon: " + myLon,
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onProviderEnabled(String arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub
        }

    }

}

which has an Inner class that implements location listener on it. So far, it works. It listen the location with no problem. However, when I exit my apps, it doesn’t completely stop the listen the location. As you see from this code :

@Override
            public void onLocationChanged(Location newLocation) {
                // TODO Auto-generated method stub
                myLat = newLocation.getLatitude();
                myLon = newLocation.getLongitude();
                getLocationName(myLat, myLon);
                Toast.makeText(context,
                        "Lokasi terupdate\nLat: " + myLat + "\nLon: " + myLon,
                        Toast.LENGTH_SHORT).show();
            }

while the location is updated, the toast will be shown, and it still appear when the location is updated. Even when I’ve closed the application.

How to stop the location listener completely in my case.

This one helped me to stop the updat :

android how to stop gps

  • 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-31T15:21:54+00:00Added an answer on May 31, 2026 at 3:21 pm

    You may call removeUpdates on your LocationManager.

    public void removeUpdates (PendingIntent intent)

    Removes any current registration for location updates of the current
    activity with the given PendingIntent. Following this call, updates
    will no longer occur for this intent.

    Parameters:

    intent {#link PendingIntent} object that no longer needs location
    updates

    Throws:

    IllegalArgumentException if intent is null

    See here: http://developer.android.com/reference/android/location/LocationManager.html#removeUpdates(android.app.PendingIntent)

    Updated:

    In this case, you may change this line:

    locationManager.requestLocationUpdates(provider, 1000, 0,
                    new MyLocationListener());
    

    to:

    MyLocationListener myLL = new MyLocationListener();
    locationManager.requestLocationUpdates(provider, 1000, 0,
                    myLL);
    

    When you want to remove your listener call as below:

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

Sidebar

Related Questions

I have a class which inherets handle. This class manages (among others) a dropdown
I have a custom UIImageView class which I use to handle multi-touch events on
I have a class which is marked with a custom attribute, like this: public
I have a class which looks something like this: public class Test { private
I have created a GPS service which reads location updates on a regular interval.
I'm having a tough time with this one, I have a class which populates
I made this small location class so that I can better handle address information
Suppose we have this code: class My_controller extends CI_Controller { private $model_name; function __construct($model_name)
I have main activity which call other object, for example GPS class. This GPS
I have an class which should send/receive data in packet form. This class contains

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.