1

I need to get current location, and after that - do next code. How can i wait while this method has finished? onLocationChanged is called automatically that why i have problem. Do someone has any ideas how to do itmore correct? I make it very stupid, in OnLocationChanged() i call onResume(), but it is so bad idea.

    @Override
public void onLocationChanged(Location location) {

    final Location loc = location;

    Log.d("myLogs", "OnChange2");
    Log.d("myLogs", "2" + loc.getLatitude() + "," + loc.getLongitude());
    myLat = loc.getLatitude();
    myLong = location.getLongitude();
    onResume();

}
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
Kostya Khuta
  • 1,846
  • 6
  • 26
  • 48

2 Answers2

2

You might want 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 and read How to Get GPS Location Using AsyncTask? .

Some other similar helpful questions:

Wait for current location - GPS - Android Dev

getting location instantly in android

Hope this helps.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • i think i must learn AsyncTask, but now what is more simple solution? – Kostya Khuta Aug 23 '13 at 02:11
  • I think sometime or the other if you would be helpful for you. It is straight forward. You can try to start with 3 basic functions doInBackground , onPreExecute and onPostExecute. onPreExecute is called just before starting background process. Things inside doInBackground are run in background thread. onPostExecute is run after the background process. – Shobhit Puri Aug 23 '13 at 02:17
  • Ok, but 1. In what thread onLocationChange() is running and where it started. 2 How can i do onLocationChange () in background if this method is calling by system? – Kostya Khuta Aug 23 '13 at 02:20
  • You can request for location updates from inside your `doInbackground` using `requestLocationUpdates` like: `LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); LocationListener mlocListener = new MyLocationListener(); mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);` – Shobhit Puri Aug 23 '13 at 02:30
0

I realize the OP has accepted the above answer but I have a feeling the OP wanted a more simple answer.

I am assuming the OP has an android application with an Activity. I declared mine like this:

public class HelloAndroidActivity extends Activity implements LocationListener {

The OP was confused as to how the lifecycle methods worked and when work should be done. My Resume and Pause methods would look like this:

@Override
protected void onPause() {
    ((LocationManager)getSystemService(Context.LOCATION_SERVICE)).removeUpdates(this);
    super.onPause();
}

@Override
protected void onResume() {
    ((LocationManager)getSystemService(Context.LOCATION_SERVICE)).requestLocationUpdates(LocationManager.GPS_PROVIDER, 5 * 1000, 1, this);
    super.onResume();
}

Notice that my onResume asks that I be notified when there are location updates and the onPause method asks that I no longer be notified. You should be careful to not ask for updates at a time interval smaller then you really need or you will drain your battery.

Since the activity implements LocationListener my onLocationChanged method looks like this:

@Override
public void onLocationChanged(Location location) {
    // Update the location fields
    ((EditText)findViewById(R.id.latField)).setText(Double.toString(location.getLatitude()));
    ((EditText)findViewById(R.id.longField)).setText(Double.toString(location.getLongitude()));
}

This simply takes the new location and updates some text EditText fields that I have in my activity. The only other thing I needed to do was to add the GPS permission to my manifest:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

So if I were asking how to start out using the location manager and the location service this would be how I would begin. I'm not trying to take anything away from the accepted answer, I just think there was a fundamental misunderstanding about what to do in onResume and in the onLocationMethodChanged methods.

ditkin
  • 6,774
  • 1
  • 35
  • 37