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.

有帮助吗?

解决方案

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;
            }
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top