Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well you could do</p> <pre><code>while (value == 0.0) { // do nothing } // now use value </code></pre> <p>but that's a really bad idea. You would get "Application Not Responding". Also <em>you must not pause your Activity,</em> never ever - and it is not necessary.</p> <p>What you should do is to add a way to notify your code that wants to use the values once they change. The easiest way with LocationListeners is implementing them directly in your Activity. like so:</p> <pre><code>class MyActivity extends Activity implements LocationListener { onCreate() { locmanag.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); } public void onLocationChanged(Location location) { // now change the ui. This is called on UI thread. } } </code></pre> <p>If you want the location to be handled by a different class you can still notify your activity about changes.</p> <p>If you want to use your LocationListener from just one Activity that would work:</p> <pre><code>class MyActivity extends Activity { private LocationGetter mGetter; onCreate() { mGetter = new LocationGetter(this); locmanag.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mGetter); } public void somethingHappend() { mGetter.getLat(); } } public class LocationGetter implements LocationListener { private final MyActivity mActivity; public LocationGetter(MyActivity activity) { mActivity= activity; } @Override public void onLocationChanged(Location location) { if(location != null){ mLat = location.getLatitude(); mLong = location.getLongitude(); if (mActivity!= null) { mActivity.somethingHappend(); } } public long getLat() { return mLat; } } </code></pre> <p>If you want to use your LocationListener from different Activities you should implement a common Interface (like LocationListener is)</p> <pre><code>class MyActivity extends Activity implements LocationGetter.MyCallback { private LocationGetter mGetter; onCreate() { mGetter = new LocationGetter(this); locmanag.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mGetter); } public void somethingHappend() { mGetter.getLat(); } } public class LocationGetter implements LocationListener { public Interface MyCallback { // you could pass lat + long here public void somethingHappend(); } private final MyCallback mCallback; public LocationGetter(MyCallback callback) { mCallback = callback; } @Override public void onLocationChanged(Location location) { if(location != null){ mLat = location.getLatitude(); mLong = location.getLongitude(); if (mCallback != null) { mCallback.somethingHappend(); } } public long getLat() { return mLat; } } </code></pre> <p>Instead of creating a new callback interface you could also use LocationListener from all your Activities. Works as well but does not show you how those callbacks work :)</p> <p>Another possbility is to use e.g. Handler (especially if the other thing is executed in a different Thread)</p> <pre><code>class MyActivity extends Activity implements Handler.Callback { private Handler mHandler; onCreate() { mHandler = new Handler(this); mGetter = new LocationGetter(mHandler); locmanag.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mGetter); } public boolean handleMessage(Message msg) { // msg.what would be 0, that's what we send mGetter.getLat(); // we are in UI thread here. safe to modify. return true; } } public class LocationGetter implements LocationListener { private final Handler mHandler; public LocationGetter(Handler handler) { mHandler = handler; } @Override public void onLocationChanged(Location location) { if(location != null){ mLat = location.getLatitude(); mLong = location.getLongitude(); if (mHandler != null) { // you can send a lot more here mHandler.sendEmptyMessage(0); } } public long getLat() { return mLat; } } </code></pre> <p>Besides waiting for LocationListener isn't even possible if you try that from your Activity UI thread since you get the update on the UI Thread which is blocked by your waiting. So you never get the update.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload