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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:02:41+00:00 2026-05-22T16:02:41+00:00

all. I am writing an android app about GPS locations. I tried it on

  • 0

all. I am writing an android app about GPS locations.

I tried it on emulator and entered the latitude and longitude manually, and it worked fine.
However, my problem is: on the real device, in debugging mode, to go the next class by using intent can only be achieved when location is changed. If I start the app directly, I can see the blinking GPS icon, but the app will only stay here, and won’t start the next activity. It seems that the variables in the onLocationChanged() will never be changed.

I have heard that to get the location instantly is to use the getLastKnownLocation() method. But I failed to get where I should use it.

Here is the class of how I use the LocationManager to get the locations.

Is there any solutions? I am quite confused. Thank you very much!!

    public class mainMenu extends Activity{

private LocationManager locationManager = null;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);




    Button button1 = (Button)findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener(){
        public void onClick(View v){

            Intent i3 = new Intent();
            i3.setClass(mainMenu.this, police.class);
            i3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mainMenu.this.startActivityForResult(i3,0);


        }
    });

    Button button2 = (Button)findViewById(R.id.button2);
    button2.setOnClickListener(new OnClickListener(){
        public void onClick(View v){

            Intent i3 = new Intent();
            i3.setClass(mainMenu.this, ambulance.class);
            i3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mainMenu.this.startActivityForResult(i3,0);


        }
    });

    Button button3 = (Button)findViewById(R.id.button3);
    button3.setOnClickListener(new OnClickListener(){
        public void onClick(View v){

            Intent i3 = new Intent();
            i3.setClass(mainMenu.this, fire_station.class);
            i3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mainMenu.this.startActivityForResult(i3,0);


        }
    });



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

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 120000 , 0, new MyLocationUpdater());

        String provider = LocationManager.GPS_PROVIDER;
        Location location = locationManager.getLastKnownLocation(provider);



}



public class MyLocationUpdater implements LocationListener{ //change location interface
    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
    //          store the location data
    //          get the best record



        Double lat = location.getLatitude();
        Double lon = location.getLongitude();

        System.out.println("The latitude is " + lat + "and " 
                + "the longitude is "+ lon);


        Double lat11 = lat - 1/69.0;
        Double lat12 = lat + 1/69.0;
        Double lon11 = lon - 1/42.0;
        Double lon12 = lon + 1/42.0;

        StaticVariables.latS1 = lat11.toString();
        StaticVariables.latN1 = lat12.toString();
        StaticVariables.lonW1 = lon11.toString();
        StaticVariables.lonE1 = lon12.toString();

        Double lat111 = lat - 2/69.0;
        Double lat121 = lat + 2/69.0;
        Double lon111 = lon - 2/42.0;
        Double lon121 = lon + 2/42.0;

        StaticVariables.latS11 = lat111.toString();
        StaticVariables.latN11 = lat121.toString();
        StaticVariables.lonW11 = lon111.toString();
        StaticVariables.lonE11 = lon121.toString();

    //          ==================================================
    //          ambulances

        Double lat21 = lat - 3/69.0;
        Double lat22 = lat + 3/69.0;
        Double lon21 = lon - 3/42.0;
        Double lon22 = lon + 3/42.0;

        StaticVariables.latS2 = lat21.toString();
        StaticVariables.latN2 = lat22.toString();
        StaticVariables.lonW2 = lon21.toString();
        StaticVariables.lonE2 = lon22.toString();

        Double lat211 = lat - 5.5/69.0;
        Double lat221 = lat + 5.5/69.0;
        Double lon211 = lon - 5.5/42.0;
        Double lon221 = lon + 5.5/42.0;

        StaticVariables.latS21 = lat211.toString();
        StaticVariables.latN21 = lat221.toString();
        StaticVariables.lonW21 = lon211.toString();
        StaticVariables.lonE21 = lon221.toString();

    //          ===================================================
    //          fire stations

        Double lat31 = lat - 2/69.0;
        Double lat32 = lat + 2/69.0;
        Double lon31 = lon - 2/42.0;
        Double lon32 = lon + 2/42.0;

        StaticVariables.latS3 = lat31.toString();
        StaticVariables.latN3 = lat32.toString();
        StaticVariables.lonW3 = lon31.toString();
        StaticVariables.lonE3 = lon32.toString();

        Double lat311 = lat - 2/69.0;
        Double lat321 = lat + 2/69.0;
        Double lon311 = lon - 2/42.0;
        Double lon321 = lon + 2/42.0;

        StaticVariables.latS31 = lat311.toString();
        StaticVariables.latN31 = lat321.toString();
        StaticVariables.lonW31 = lon311.toString();
        StaticVariables.lonE31 = lon321.toString();

        Intent intent = new Intent();
        intent.setClass(mainMenu.this, getPhoneNumber.class);
        mainMenu.this.startActivity(intent);
    }

    @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

    }
}

    }
  • 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-22T16:02:42+00:00Added an answer on May 22, 2026 at 4:02 pm

    The getLastKnownLocation method does not trigger an onLocationChanged event. One way to refactor your code would be to move the logic that acts on a Location to a separate method and then call that method both after you call getLastKnownLocation, and from your onLocationChanged method.

    Bear in mind that there is no guarantee that getLastKnownLocation will provide a meaningful Location, since you the device might have moved since the last location update.

    Example code:

    public void onCreate(Bundle savedInstanceState){
        ....
        String provider = LocationManager.GPS_PROVIDER;
        Location location = locationManager.getLastKnownLocation(provider);
        updateLocation(location);
    }
    
    public class MyLocationUpdater implements LocationListener{ //change location interface
        @Override
        public void onLocationChanged(Location location) {
            updateLocation(location);
        }
        ...
    }
    
    void updateLocation(Location location) {
        Double lat = location.getLatitude();
        Double lon = location.getLongitude();
    
        // the rest of the code from onLocationChanged
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing my first location based android app, but got confused about some of
I am writing a android app that will pull in a list of all
All, I'm writing a native application for Android and i'm stuck trying to acquire
I'm writing a simple XMPP Client in android, I've extracted all the XMPP Calls
I'm writing an Android app. I have a main method, which creates and runs
I'm writing an Android app to keep up with shopping lists. The main view
I'm writing an android app that has a spinner in it for difficulty settings
I'm writing an android app where you have a recursive function that takes a
I'm writing an Android app and I have a class that generates and maintains
I'm writing an Android app that is parsing RSS feeds from different sources. What

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.