15

所以我实现了新的 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;
    }
}
4

3 回答 3

11

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

于 2016-11-10T16:38:05.837 回答
3

I think I have found the solution of the problem.

If you go to Settings -> Privacy and Safety -> Location, you would notice that Location is not only GPS, but it actually lets user decide which providers can be used. For example, you can set that only WiFi and Cellular should be used to obtain any locations

Disabling Location option will disable all providers at once, not only GPS.

To test that app for users with only WiFi can get a location – change setting to "Wifi+Cellular".

于 2016-09-17T20:30:17.260 回答
0

您似乎正在使用LocationRequest.PRIORITY_HIGH_ACCURACY,它更喜欢在上面和其他方法中使用 GPS。您可能想要使用PRIORITY_BALANCED_POWER_ACCURACY,它将在使用 GPS 之前使用 WiFi 和手机信号塔。

此外,由于PRIORITY_BALANCED_POWER_ACCURACY是“粗略”级别的准确度,您可能需要适当地更改清单中的位置权限。

培训文档详细介绍了有关优先级标志的更多信息,您可能还想仔细阅读它。

于 2015-02-19T04:34:21.970 回答