融合位置提供程序无法获取位置,除非 GPS 处于打开状态

因此,我实现了新的融合位置提供程序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;
    }
}

答案 1

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


答案 2

我想我已经找到了问题的解决方案。

如果您转到“设置”->“隐私和安全”->位置,您会注意到“位置”不仅是GPS,而且它实际上允许用户决定可以使用哪些提供商。例如,您可以设置只应使用 WiFi 和蜂窝网络来获取任何位置

禁用位置选项将一次禁用所有提供商,而不仅仅是GPS。

要测试该应用,只有WiFi的用户可以获得位置 - 将设置更改为“Wifi +蜂窝”。


推荐