简体   繁体   English

等待方法完成后再返回值

[英]Wait for method to complete before returning value

I've found quite a few answers to similar questions, but have yet to find one that works for me. 我已经找到了很多类似问题的答案,但是还没有找到适合我的答案。 I have a method in a helper class that returns a String value of a users present latitude. 我在帮助程序类中有一个方法,该方法返回当前纬度用户的String值。 here is the code. 这是代码。

public String getLatitude(){
    createLocationListener();
    defineCriteria();
    createLocationManager();
    getLocation();
    return String.valueOf(d_latitude);
}

My issue is that class variable d_latitude is updating correctly in the getLocation() or more specifically the onLocationChanged() method, but getLatitude() is returning a null d_latitude before getLocation() is complete. 我的问题是类变量d_latitudegetLocation()或更确切地说是onLocationChanged()方法中正确更新,但是getLatitude()getLocation()完成之前返回空的d_latitude Like I said, the solutions I have found didn't work for me for whatever reason or they seemed totally too complex for my situation (200 line special classes and such). 就像我说的那样,无论出于什么原因,我发现的解决方案都不适合我,或者对于我的情况,它们似乎太复杂了(200行特殊类等)。

private void getLocation(){
    //get single shot location update, null looper sets single shot
    locationManager.requestSingleUpdate(criteria, locationListener, null);
}


private void createLocationListener(){
    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(android.location.Location location){
            d_latitude = location.getLatitude();
            Toast.makeText(Location.this, "olc lat= " +d_latitude, Toast.LENGTH_SHORT).show();
            stopLocationListener();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras){
        }
        @Override
        public void onProviderEnabled(String provider){
        }
        @Override
        public void onProviderDisabled(String provider){
        }
    };
}

You need to understand how async functions and listeners work together. 您需要了解异步功能和侦听器如何协同工作。

For example, if you changed your method 例如,如果您更改了方法

private void getLocation(LocationListener l) {
    locationManager.requestSingleUpdate(criteria, l, null);
}

Then you could pass in the listener, which calls back to the method, and "waits" for a response on a different thread. 然后,您可以传入侦听器,该侦听器将回调该方法,并“等待”另一个线程上的响应。

You could define your own callback interface to get the latitude externally. 您可以定义自己的回调接口以从外部获取纬度。

An example would be 一个例子是

public interface MyLocatonListener {
    void onLatChanged(Latitude lat);
    void onLongChanged(Longitude lng);
} 

And you pass an instance of that around, but, continuing on without that, since you already have a builtin listener... 然后您传递了一个实例,但是继续进行下去,因为您已经有了内置的侦听器...

Since you're not guaranteed an immediate response, you cannot return from this method, so make it void 由于不能保证立即响应,因此无法从此方法return ,因此请使其无效

public void getLatitude(){
    defineCriteria();
    createLocationManager();
    getLocation(new LocationListener() {
        @Override
        public void onLocationChanged(android.location.Location location){
            // latitude is "returned" here 
            d_latitude = location.getLatitude();
            Toast.makeText(Location.this, "olc lat= " +d_latitude, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras){
        }
        @Override
        public void onProviderEnabled(String provider){
        } 
        @Override
        public void onProviderDisabled(String provider){
        }
    });
    // d_latitude == null here, most likely 
}

For another example, you can extract the LocationListener parameter up to the getLatitude method, then pass it down into getLocation 对于另一个示例,您可以将LocationListener参数提取到getLatitude方法之前,然后将其向下传递到getLocation

public void getLatitude(LocationListener l){
    defineCriteria();
    createLocationManager();
    getLocation(l);
}

By the way, I think you can define criteria and location manager before this method 顺便说一句,我认为您可以在此方法之前定义条件和位置管理器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM