gpt4 book ai didi

java - 除非 GPS 开启,否则融合位置提供程序无法获取位置

转载 作者:搜寻专家 更新时间:2023-10-30 21:08:44 26 4
gpt4 key购买 nike

因此,我已经实现了新的 Fused Location Provider API 来获取用户的位置,但出于某种原因,除非打开 GPS,否则我无法获取任何位置。并非总是如此,用户会打开他们的 GPS,我不想在每次加载应用程序时都要求他们打开他们的 GPS。我怎样才能告诉 API 向我提供它可用的任何提供商的位置?

这是我的代码:

public class FusedLocationService implements
LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {

public interface OnLocationChangedListener {
public void OnLocationChanged(Location location);
}

private final String TAG = "SE8611";
private boolean mRequestingLocationUpdates = true;

private OnLocationChangedListener mCallBack;

Service locationService;
private LocationRequest locationRequest;
private GoogleApiClient googleApiClient;
private Location mCurrentLocation;
private FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;

public FusedLocationService(Service locationService, final long INTERVAL, final long FASTEST_INTERVAL) {
Logger.log(TAG, "FusedLocationService called");

this.mCallBack = (OnLocationChangedListener)locationService;

locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(INTERVAL);
locationRequest.setFastestInterval(FASTEST_INTERVAL);
this.locationService = locationService;

googleApiClient = new GoogleApiClient.Builder(locationService)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();

if (googleApiClient != null) {
googleApiClient.connect();
}
}

@Override
public void onConnected(Bundle connectionHint) {
if(mRequestingLocationUpdates) {
startLocationUpdates();
}else{
Logger.log(TAG, "Location updates are already running.");
}
}

protected void startLocationUpdates() {
this.fusedLocationProviderApi.requestLocationUpdates(
googleApiClient, locationRequest, this);
this.mRequestingLocationUpdates = false;
}

@Override
public void onLocationChanged(Location mCurrentLocation) {
Logger.log(TAG, "onLocationChanged called");
this.mCurrentLocation = mCurrentLocation;
this.mCallBack.OnLocationChanged(this.mCurrentLocation);
}

public void startLocationUpdatesAfterResume(){
if (googleApiClient.isConnected() && !mRequestingLocationUpdates) {
Logger.log(TAG, "startLocationUpdatesAfterResume called");
this.startLocationUpdates();
}
}

public void stopLocationUpdates() {
Logger.log(TAG, "stopping Location Updates");
LocationServices.FusedLocationApi.removeLocationUpdates(
googleApiClient, this);
}

public Location getLocation() {
return this.mCurrentLocation;
}

@Override
public void onConnectionSuspended(int i) {
this.mRequestingLocationUpdates = true;
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
this.mRequestingLocationUpdates = true;
}
}

最佳答案

我和你有同样的问题,这根本不是问题。Android 曾经有一个 GPS 按钮,可以让你直接控制它,但他们用一个工作方式不同的位置按钮取代了它。为了获得任何类型的位置,您必须将其打开。和您一样,我认为位置按钮只能打开和关闭 GPS,但事实并非如此。您可以通过更改定位模式来控制 GPS: 1. 高精度(GPS、Wi-Fi 和移动网络) 2. 省电(Wi-Fi 和移动网络) 3. 仅 GPS

关于java - 除非 GPS 开启,否则融合位置提供程序无法获取位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28492122/

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