Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need android activity to wait until GPS location obtained

Sorry for my english. I'm trying to get a single location from GPS to put on global variables latitude, longitude. GPS turns on, but the activity goes on before data is retrieved from GPS.

My needs in other words... method getCurrentLocation() must finish only if a location has been found and the longitude and latitude variables are filled, so I could use them in other method. I know... user has to wait... I will solve this forward showing something on screen. What should I do? Thank you

I think I'm skipping stop listening GPS at some place. Where is better?

Code follows:

//Method to retrieve coordinates
public void getCurrentLocation() {
    //Use GPS if possible
    if(manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        //assign Listener to GPS
        manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
        Toast.makeText(this, LocationManager.GPS_PROVIDER, Toast.LENGTH_SHORT).show();
    }
    else if(manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){//toherwise, use NETWORK
        //assign Listener to NETWORK
        manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
        Toast.makeText(this, LocationManager.NETWORK_PROVIDER, Toast.LENGTH_SHORT).show();
    }
}

//Class to store the location recived in two variables
final class MyLocationListener implements LocationListener {

    @Override
    public void onLocationChanged(Location location) {
        //coordinates storing
        latitude = String.valueOf(location.getLatitude());
        longitude = String.valueOf(location.getLongitude());
        Toast.makeText(getApplicationContext(), latitude + longitude, Toast.LENGTH_LONG).show();
    }

    @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
    }
}
like image 539
user2619546 Avatar asked Jul 25 '13 16:07

user2619546


1 Answers

If you want android activity to wait until GPS location obtained, you might try to use AsyncTask for that. See Android find GPS location once, show loading dialog for the answers. Basically in onPreExecute you can start dialog( it starts before the doInBackground is called). It means you are waiting till the time you can location and showing the dialog. Then in doInBackground you can get the location. After that finishes onPostExecute is called. You can stop is from inside onPostExecute. You can check if the location is not null and then call some other function from inside onPostExecute also if you want.

This might be one way. You can learn a basic example from AsyncTask Android example . You can also start by reading the documentation here.

Some other similar helpful questions:

Wait for current location - GPS - Android Dev

getting location instantly in android

Hope this helps.

like image 160
Shobhit Puri Avatar answered Sep 28 '22 00:09

Shobhit Puri