gpt4 book ai didi

java - Android FusedLocationApi.getLastLocation 同步

转载 作者:行者123 更新时间:2023-11-30 00:30:56 26 4
gpt4 key购买 nike

我需要将异步 FusedLocationApi.getLastLocation 调用转换为同步调用。我不是 Java 的大专家,我不确定我做对了。

我想连接到 GoogleApiClient,然后阻止调用线程,直到收到位置信息或错误。超时超过 10 秒应视为错误。

它会以这种方式工作吗,或者 onConnected 也会在主线程上被调用并在那个时候被锁定?

我还注意到 lock.wait(TimeUnit.SECONDS.toMillis(10)); 在常规运行中会立即继续,但在逐步调试中它会按预期等待 10 秒。

public class SyncLocationService implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

public GoogleApiClient mGoogleApiClient;

public SyncLocationService(Context context) {
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}

private final Object lock = new Object();
public android.location.Location mLocation;

@Override
public android.location.Location lastLocation() {
mGoogleApiClient.connect();
synchronized (lock) {
try {
lock.wait(TimeUnit.SECONDS.toMillis(10));
} catch (InterruptedException e) {
Log.i("NativeLocationService", "InterruptedException");
}
}
return mLocation;
}

@Override
public void onConnected(@Nullable Bundle bundle) {
try {
mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
mGoogleApiClient.disconnect();
} catch (SecurityException e) {
Log.i("NativeLocationService", "SecurityException");
} finally {
synchronized (lock) {
lock.notify();
}
}
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
synchronized (lock) {
lock.notify();
}
}
}

基本上我正在尝试重写类似的 iOS 代码,它使用信号量来完成它的工作。

var semaphore: DispatchSemaphore!
var location: CLLocation!

func lastLocation() -> location? {
self.semaphore = DispatchSemaphore(value: 0)
self.locationManager.startUpdatingLocation()
_ = self.semaphore!.wait(timeout: .now() + 10) // seconds
self.semaphore = nil
return self.location
}

// MARK: - CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let semaphore = self.semaphore else {
return
}
guard let first = locations.first else {
return
}
self.location = first
self.locationManager.stopUpdatingLocation()
semaphore.signal()
}

最佳答案

您可以使用 GoogleApiClient.blockingConnect() 等待连接建立,然后调用 FusedLocationApi.getLastLocation()

关于java - Android FusedLocationApi.getLastLocation 同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44397647/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com