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在哪裏? –