14

按照Android文档启用:安卓:检查位置服务使用一体化位置提供

的谷歌位置服务API,谷歌Play的一部分服务, ,可自动提供更强大的,高层次的框架 处理位置提供者,用户移动和位置准确性比 平台位置API android.location

但使用融合位置提供程序(来自Google Play服务中的位置API)我不知道如何检查用户是否启用或禁用位置。

使用android.location很容易:

locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

但我不希望同时使用谷歌播放融合的位置信息提供和的Android位置服务。

我该如何检查用户是否使用熔合位置提供程序启用位置?

在此先感谢。

+0

嗨,你可以请你的实施作为答案吗? –

回答

17

请参阅SettingsApi:检查您的位置请求,然后确保设备的系统设置已针对应用的位置需求进行了正确配置。

+0

我在这里实现了SettingsClient:https://github.com/askfortricks/FusedLocationProviderClient,因为现在SettingsApi已被弃用。 –

35

This android developer training tutorial could help - 这里的基本知识:

// Create an instance of GoogleAPIClient. 
    if (mGoogleApiClient == null) { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
    } 

    mGoogleApiClient.connect(); 

    LocationRequest mLocationRequest = new LocationRequest(); 
    mLocationRequest.setInterval(10000); 
    mLocationRequest.setFastestInterval(5000); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() 
      .addLocationRequest(mLocationRequest); 

    PendingResult<LocationSettingsResult> result = 
      LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build()); 

    result.setResultCallback(new ResultCallback<LocationSettingsResult>() { 
     @Override 
     public void onResult(LocationSettingsResult locationSettingsResult) { 

      final Status status = locationSettingsResult.getStatus(); 
      final LocationSettingsStates LS_state = locationSettingsResult.getLocationSettingsStates(); 
      switch (status.getStatusCode()) { 
       case LocationSettingsStatusCodes.SUCCESS: 
        // All location settings are satisfied. The client can initialize location 
        // requests here. 

        break; 
       case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 
        // Location settings are not satisfied. But could be fixed by showing the user 
        // a dialog. 
        try { 
         // Show the dialog by calling startResolutionForResult(), 
         // and check the result in onActivityResult(). 
         status.startResolutionForResult(LocationByGPS.this, REQUEST_CHECK_SETTINGS); 

        } catch (IntentSender.SendIntentException e) { 
         // Ignore the error. 
        } 
        break; 
       case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: 
        // Location settings are not satisfied. However, we have no way to fix the 
        // settings so we won't show the dialog. 

        break; 
      } 
     } 
    }); 

重写此方法:

代码在你活动的onCreate()运行

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    final LocationSettingsStates states = LocationSettingsStates.fromIntent(data); 
    switch (requestCode) { 
     case REQUEST_CHECK_SETTINGS: 
      switch (resultCode) { 
       case Activity.RESULT_OK: 
        // All required changes were successfully made 
        GetUserLocation();//FINALLY YOUR OWN METHOD TO GET YOUR USER LOCATION HERE 
        break; 
       case Activity.RESULT_CANCELED: 
        // The user was asked to change settings, but chose not to 

        break; 
       default: 
        break; 
      } 
      break; 
    } 
} 

记得要实现这些在你的课堂上:

public class MyClass extends AppCompatActivity implements 
    ActivityCompat.OnRequestPermissionsResultCallback, 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener{ 

    protected static final int REQUEST_CHECK_SETTINGS = 0x1; 


    /* 
    your code i.e. with the above code etc.... 
*/ 

} 

Good explanation here in this Google developer link.

干杯!

+0

我已经编辑了我的答案,通过添加代码和另一个有用的链接。 –

+0

看起来不错 - 我做了一些清理并修复了你的链接,但否则这是一个可靠的答案。感谢您接受建议,祝您好运! – Mogsdad

+0

LocationByGPS.this在哪里? –