문제

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