How to make Android wait?
Asked Answered
M

3

1

In my program, it is needed to receive user latitude and longitude as soon as GPS is Enabled... I'm using onGpsStatusChanged to constantly check GPS Status and the checking works... The only problem is that it tries to retrieve user location instantly, and it returns latitude 0.0 and longitude 0.0 cause it takes some seconds to get those informations... If I try to retrieve some seconds later using clickListener on a button it works perfectly...

Then I thought: If I could make the device wait some seconds after GPS is Enabled and only then retrieve the coordinates it would work...

But: If I use another Thread or AsyncTask (already tried both), it keeps returning 0.0 for coordinates, cause the real coordinates are cached into the Map's Main Activity Thread...

So, how to make android wait in the main Thread? I've tried 'wait(long)' and the app crashes. I'm trying to solve this for weeks and my time is running out... Some holy soul help me please

Mudcat answered 16/10, 2016 at 6:24 Comment(5)
developer.android.com/reference/android/location/… I have just read a little, and it seems you can request update with pending intent.Inflame
You could try posting a delayed runnable with a handler and ensure it runs on ui thread.Scapegrace
dont make UI thread Wait, its bad. If you have used AsyncTask, then do it your onGpsStatusChanged in doInBackground() and update your co-ordinates inside the onPostExecute(), this runs on the main UI thread and is automatically after doInBackground(). This way you dont have to explicitly make the UI thread waitGrooms
@PaulKaram, I wouldn't know how to do that... I'm pretty newbie to Android developing... shubhankars, Mohamed exemplified it for me and it worked perfectly! Thank u very muchMudcat
Whoa, I haven't thought this solution... I was updating coordinates on doInBackground(), I guess it would also work... Thank you!Mudcat
D
1

you can achieve that by using handler

int time=3000 // in milliseconds

      Handler h=new Handler();

      h.postDelayed(new Runnable() {

         @Override
        public void run() {

         //here you can do the job

          }

        },time);

if you want to update the UI from the handler you will end up with an error but you can userunOnUiThread

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        //code to update the ui
    }
});

good luck

Dromond answered 16/10, 2016 at 6:33 Comment(1)
OMG! 2 weeks of noobing == 15 min of expert talking... It just worked perfectly! I really appreciate your help!Mudcat
S
1

i not recommend you handler,you can use Interface here. where you get all values use pass it to your activity and use that one.

Here i have posted example just try to follow and use this way.

You can pass interval here according to your need.

public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 30000

and one more thing you can pass fastest interval that is also help you.

Thanks

Scarab answered 16/10, 2016 at 6:53 Comment(0)
D
0

I have been creating a simple Waiter class for it. It is really easy to understand and does the job very well. I post it for those who need it. just copy Waiter class and WaitListener interface and use it like I show.

Here is the Class:

import android.os.Handler;
import android.os.Looper;

public class Waiter {

WaitListener waitListener;
int waitTime = 0;
Handler handler;
int waitStep = 1000;
int maxWaitTime = 5000;
boolean condition = false;

public Waiter(Looper looper, final int waitStep, final int maxWaitTime){

    handler = new Handler(looper);
    this.waitStep = waitStep;
    this.maxWaitTime = maxWaitTime;

}

public void start(){

    handler.post(new Runnable() {
        @Override
        public void run() {

            waitListener.checkCondition();

            if (condition) {

                waitListener.onConditionSuccess();

            } else {
                if (waitTime <= maxWaitTime) {

                    waitTime += waitStep;
                    handler.postDelayed(this, waitStep);

                } else {

                    waitListener.onWaitEnd();
                }
            }
        }
    });

}

public void setConditionState(boolean condition){
    this.condition = condition;
}

public void setWaitListener(WaitListener waitListener){
    this.waitListener = waitListener;
}

}

And here is the Interface :

public interface WaitListener {

public void checkCondition();

public void onWaitEnd();

public void onConditionSuccess();

}

You can for example use it like that for a connection check:

ConnectivityManager mConnMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final int result = mConnMgr.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, "enableMMS");

final Waiter waiter = new Waiter(getMainLooper(), 1000, 5000);
waiter.setWaitListener(new WaitListener() {

            @Override
            public void checkCondition() {
                Log.i("Connection", "Checking connection...");
                NetworkInfo networkInfo = mConnMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_MMS);
                waiter.setConditionState(networkInfo.isConnected());
            }

            @Override
            public void onWaitEnd() {
                Log.i("Connection", "No connection for sending");
                //DO
            }

            @Override
            public void onConditionSuccess() {
                Log.i("Connection", "Connection success, sending...");
                //DO
            }

});

waiter.start();
Dimorphous answered 21/7, 2018 at 12:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.