Question

I am doing a lot of job in onLocationChanged method, i want to lock the method until all jobs are done even if the location manager achieves it's condition.

for example i have the following code:

private LocationManager _locMgr;
protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
                _locMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            _locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 2, this);
}

public void onLocationChanged(Location location) {

//do alot of job

 }

The problem i am facing that if the time in locationManager is greater than 2 seconds and greater than 2 meters it'll execute the onLocationChanged() even if there's tasks not finished.

Please somebody help me with this.

Was it helpful?

Solution

If you are sure that onLocationChanged() is called before finished, try this:

Declare an Activity field private bolean isAllDone = true;
Declare a lock object private final Object lock = new Object();

Then at onLocationChange test it:

public void onLocationChanged(Location location) {

    if(isAllDone){
        synchronized (lock){
            if(isAllDone){
                isAllDone = false;
                //do alot of job
                isAllDone = true;
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top