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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:48:53+00:00 2026-06-14T22:48:53+00:00

In my current application, the location data that is returned is outdated. I use

  • 0

In my current application, the location data that is returned is outdated. I use a method that gets updates based on min time/distance between updates. However, lets say i turn my phone off, drive to another city, and then turn the phone back on. In this case my GPS reading will be off. How do i force the app to get the current location NOT getLastKnownLocation().

I have heard of a locationListener but everything i have read is very vague on how to use it.

Here is the code I have just in case this clarifies whats going on:

public class GPSHandling extends Service implements LocationListener{

    private final Context myContext;

    //flag for gps status
    public boolean isGPSEnabled = false;

    //flag for network status
    public boolean isNetworkEnabled = false;

    // used to determine if i can get a location either by network or GPS
    public boolean canGetLocation = false;

    Location myloc;

    public double latitude;
    public double longitude;

    public int MIN_TIME_BTWN_UPDATE = 500*10;  // in miliseconds, so 10sec 
    public int MIN_DISTANCE_BTWN_UPDATE = 10;  // in meters 
    protected LocationManager locManager;

    public GPSHandling(Context context){
        this.myContext= context;
        getLocation();
    }
    public Location getLocation(){
        try{
            locManager =(LocationManager) myContext.getSystemService(LOCATION_SERVICE);

            // now get gps status
            isGPSEnabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

            //now the same for network
            isNetworkEnabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled){
                // do nothing since neither provider are enabled (this.cangetlocation = false but its already set to that by default)
            }
            else{
                this.canGetLocation=true;
                //first get values for locManager from the network provider. send parameters telling when to update
                if(isNetworkEnabled){
                    locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BTWN_UPDATE, MIN_DISTANCE_BTWN_UPDATE, this);
                    Log.d("provider", "network");
                    //if we are successful, then check to see if the location manager isnt null. attempt to get the current location from the manager
                    if (locManager != null){
                        myloc =locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                            // after getting the current location, attempt to get the latitude and longitude values
                            if (myloc != null){
                                latitude = myloc.getLatitude();
                                longitude = myloc.getLongitude();
                                              }
                                           }
                                    }
                //now get values for locManager from the GPS provider. send parameters telling when to update
                if(isGPSEnabled){
                    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BTWN_UPDATE, MIN_DISTANCE_BTWN_UPDATE, this);
                    Log.d("provider", "GPS");
                }
                //if we are successful, then check to see if the location manager isnt null. attempt to get the current location from the manager
                    if(locManager!= null){
                        myloc = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    }
                    // after getting the current location, attempt to get the latitude and longitude values
                        if(myloc != null){
                            latitude = myloc.getLatitude();
                            longitude = myloc.getLongitude();
                        }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return myloc;
    }
    // get an update of the current latitude by using this class method
    public double getMyLatitude(){
        if (myloc!= null){
            latitude = myloc.getLatitude();
        }
        return latitude;
    }
    //get an update of the current longitude by using this class method
    public double getMyLongitude(){
        if (myloc != null ){
            longitude = myloc.getLongitude();
        }
        return longitude;
    }

    // use this method to find if app can get the current location
    public boolean canGetMyLocation(){
        return this.canGetLocation;
    }

    public void showGPSDialog(){
        AlertDialog.Builder alert = new AlertDialog.Builder(myContext);
        // Setting Dialog Title 
        alert.setTitle("Location Setting");
        // Setting Dialog Message
        alert.setMessage("GPS is not enabled, do you want to enable this now in the settins menue?");

        // setting icon to dialog
        //alert.setIcon(R.drawable.)

        // on pressing settings button
        alert.setPositiveButton("Settins", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                myContext.startActivity(intent);
            }
        });
        //on pressing cancel button
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        // showing the alert dialog
        alert.show();
    }

    public void stopUsingGPS(){
        if(locManager !=null){
            locManager.removeUpdates(GPSHandling.this);
        }
    }


    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

    }

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

    }

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

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}
  • 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-14T22:48:54+00:00Added an answer on June 14, 2026 at 10:48 pm

    You are already half using LocationListener in your code. You just didn’t fully implement the code.

    This method bellow is empty in your code:

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
    
    }
    

    This is the method that is called everytime GPS produces an location update.

    You should complete it doing something like:

    @Override
    public void onLocationChanged(Location location) {
        latitude = location.getLatitude();
        longitude = location.getLongitude();
    }
    

    Regards.

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

Sidebar

Related Questions

i have created an application which show data from user current location. I want
I am using FakeGps an application that spoofs your current location. I want to
I am currently in the process of creating an application that records current location
I have an android application. Based on the current geo location of user, I
i have an iOS application witch uses the current location of the user. I
I want to find out current location when the Application is running in background
I'm able to retrieve the current location in my iPad application using, CLLocation *location
whenever I try to return the lat/long coordinates for my current location, my application
My current application needs to get data from a file to initialize its attributes.
The current application that I'm working on seems to recycle the application pool very

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.