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

The Archive Base Latest Questions

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

I want to click a button and receive the current location, i understand that

  • 0

I want to click a button and receive the current location, i understand that i can’t instantly get the location , so this is what i did :
the click event :

        public void onClick(View v)
        {
            ProgressDialog MyDialog = ProgressDialog.show( MainPage.this, " " , " Loading. Please wait ... ", true);
            MyActionsHandler myActionsHandler = new myActionsHandler(MainPage.this);
            myActionsHandler.startSearch();
            MyDialog.dismiss();
            Intent intent = new Intent(MainPage.this, ResultPage.class);
            startActivity(intent);
        }

and this is the handler that searches for the location

    public void startSearch(long timeInterval,float distanceInterval)
{
    LocationManager lm = (LocationManager)_context.getSystemService(Context.LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, timeInterval,
            distanceInterval, this);

    while(!_locationFound)
    {
        //wait till location is found
    }
}

public void onLocationChanged(Location location)
{
    if (location != null)
    {
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        float speed = location.getSpeed();
        float bearing = location.getBearing();

        Log.d("LOCATION CHANGED", location.getLatitude() + "");
        Log.d("LOCATION CHANGED", location.getLongitude() + "");
        try
        {
            doTheProcess(_searchType,latitude, longitude, speed, bearing);
           _locationFound = true;
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

I understand that this doesn’t work, because the loop is in the same thread,
so what do you suggest the best solution to do it?

in the javadoc of requestLocationUpdates , there is “The calling thread must be a Looper thread such as the main thread of the calling Activity.” but i haven’t found any example so i don’t know if it’s the right solution.

one more question,
does the getLastKnownLocation() work even i fi never called the locationManager before?
thanks

  • 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-26T15:53:12+00:00Added an answer on May 26, 2026 at 3:53 pm

    I have same problem before..but I have got the solution..this is the simplest way to get location instantly.

    public class LocationFinder extends Activity {
    
        TextView textView1;
        Location currentLocation;
        double currentLatitude,currentLongitude;
    
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            textView1 = (TextView) findViewById(R.id.textView1);
            Log.i("@@@@@@@@@@ Inside LocationFinder onCreate", "LocationFinder onCreate");
    
            FindLocation();
    
        }
    
        public void FindLocation() {
            LocationManager locationManager = (LocationManager) this
                    .getSystemService(Context.LOCATION_SERVICE);
    
            LocationListener locationListener = new LocationListener() {
                public void onLocationChanged(Location location) {
                    updateLocation(location);
    
                    Toast.makeText(
                            LocationFinder.this,
                            String.valueOf(currentLatitude) + "\n"
                                    + String.valueOf(currentLongitude), 5000)
                            .show();
    
                    }
    
                public void onStatusChanged(String provider, int status,
                        Bundle extras) {
                }
    
                public void onProviderEnabled(String provider) {
                }
    
                public void onProviderDisabled(String provider) {
                }
            };
            locationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    
        }
    
    
        void updateLocation(Location location) {
                currentLocation = location;
                currentLatitude = currentLocation.getLatitude();
                currentLongitude = currentLocation.getLongitude();
                textView1.setText(String.valueOf(currentLatitude) + "\n"
                        + String.valueOf(currentLongitude));
    
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When click a button,I want to popup a view form bottom,like this! Any help
I want to add a UIViewController on my current UIView on a button click
I have an application page. I want to click a button to pop out
I have two tables on a webform. On a button click I want to
I want to add a click able button to a MKAnnotation as seen in
I want to be able to click a button and open a new form
I want to dismiss keyboard by click dismiss button ,How to build a bar
I want to finish the Activity on Home Button Click . I have below
I want to add a ZedGraph when I click a button, but the ZedGraph
I want to change data on listview runtime after button click... here is my

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.