2016-05-15 36 views
1

在文檔here我下面如何接收位置請求和兩個不同尋常的事情,我注意到在這些代碼兩大塊:當接收到位置請求時,LocationSettingsState類會做什麼?

result.setResultCallback(new ResultCallback<LocationSettingsResult>() { 
    @Override 
    public void onResult(LocationSettingsResult result) { 
     final Status status = result.getStatus(); 
     final LocationSettingsStates = result.getLocationSettingsStates(); //<--This line I don't understand 
     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(
         OuterClass.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; 
     } 
    } 
}); 

而在第二塊:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    final LocationSettingsStates states = LocationSettingsStates.fromIntent(intent); //<--And this line 
    switch (requestCode) { 
     case REQUEST_CHECK_SETTINGS: 
      switch (resultCode) { 
       case Activity.RESULT_OK: 
        // All required changes were successfully made 
        ... 
        break; 
       case Activity.RESULT_CANCELED: 
        // The user was asked to change settings, but chose not to 
        ... 
        break; 
       default: 
        break; 
      } 
      break; 
    } 
} 

我已經添加了一些指向我不明白的線條的箭頭。尤其是,這兩個:

final LocationSettingsStates = result.getLocationSettingsStates();

final LocationSettingsStates states = LocationSettingsStates.fromIntent(intent);

第一行是我以前沒有見過。這是如何有效的,爲數據類型賦值?那麼這個類不再被用在這個代碼塊的其他地方,那麼這個任務的目的是什麼呢?

在另一行中,現在它將爲該數據類型的一個名爲states的實例分配一個值,但該實例在onActivityResult()的其他任何地方都沒有使用。

那麼這裏發生了什麼?謝謝。

回答

0

第一行絕對是錯字;它應該是這樣的:

final LocationSettingsStates states = result.getLocationSettingsStates(); 

,是的,它是不是在任何地方,這是奇怪的,不過你可以調用thingsisBleUsable()它來確定究竟是存在和可用的現在。在後一種情況下,它是在嘗試解決後可用的。

相關問題