enginar enginar - 30 days ago 18
Android Question

If value is null waiting for run function in android

I run a class LokasyonBulucu()

LokasyonBulucu lokasyonBulucu= new LokasyonBulucu();
lokasyonBulucu.LokasyonBul(context);


I take two variable from this class lat and lon

lat=lokasyonBulucu.location.getLatitude();
lon= lokasyonBulucu.location.getLongitude();


but I want to waiting class found coordinates... because it take amount time.
if it find lat and lon I want to run this function

new arkaPlanIsleri(kategori_id, lat , lon).execute();

Answer Source

Use a for loop to check every second with a maximum number of seconds to wait. Check every second if the lat and lon are already found. If so execute the method and break out of the waiting loop:

for(int i = 0; i < 10; i++) { //maximum 10 seconds to wait
    if(lat != null && lon != null) { //check if the lat and lon are already found
        new arkaPlanIsleri(kategori_id, lat , lon).execute();
        break; //stop the waiting loop
    }
    SystemClock.sleep(1000); //wait one second
}