2016-04-25 158 views
0

我目前正在嘗試實現將要求更改位置設置的代碼。我目前正在關注Android Studio Tutoriel。更改位置設置 - Android教程

但是,當我按照教程將代碼放入實際Studio中時,發現它出現了一些錯誤。

protected void createLocationRequest() { 
     LocationRequest mLocationRequest = new LocationRequest(); 
     mLocationRequest.setInterval(10000); 
     mLocationRequest.setFastestInterval(5000); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     LocationSettingsRequest.Builder LSBuilder = new LocationSettingsRequest.Builder(); 
     LSBuilder.addLocationRequest(mLocationRequest); 
     final PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient,LSBuilder.build()); 
     result.setResultCallback(new ResultCallback<LocationSettingsResult>() { 
      @Override 
      public void onResult(LocationSettingsResult result) { 
       final Status status = result.getStatus(); 
       final LocationSettingsStates = result.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 this can be fixed 
         // by showing the user a dialog. 
         try { 
          // Show the dialog by calling startResolutionForResult(), 
          // and check the result in onActivityResult(). 
          status.startResolutionForResult(
            MainActivity.this, 
            REQUEST_CHECK_SETTINGS); 
         } catch (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; 
       } 
      } 
     }); 

首先,

final LocationSettingsStates = result.getLocationSettingsStates(); 

想出了意外的標記的誤差。我不太確定這裏有什麼問題。似乎還有任何其他信息。其次,

status.startResolutionForResult(MainActivity.this,REQUEST_CHECK_SETTINGS); 

在那裏,REQUEST_CHECK_SETTINGS是未定義的。我不太清楚我應該如何定義它或什麼。

任何人都可以解釋上述兩個錯誤?

回答

1

LocationSettingsStates只是一個類型,你需要指定在哪裏存儲類型的對象。替換:

final LocationSettingsStates = result.getLocationSettingsStates(); 

有:

final LocationSettingsStates states = result.getLocationSettingsStates(); 

我相信REQUEST_CHECK_SETTINGS正是您回調設置一個唯一的標識符,那麼在你的類聲明爲:

private final int REQUEST_CHECK_SETTINGS = 0; // a unique identifier