2016-04-30 57 views
0

所以我有問題的LocationSettingsRequest對話框顯示何時GPS未打開。LocationSettingsRequest對話框從不顯示

我一直在關注 SettingsApi Documentation

這裏是代碼:

enableGpsSetting:

public void enableGpsSetting(){ 
    if (mGoogleApiClient == null) { 

     mGoogleApiClient = new GoogleApiClient.Builder(context) 
       .addApi(LocationServices.API) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .build(); 
     mGoogleApiClient.connect(); 

     LocationRequest locationRequest = LocationRequest.create(); 
     locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     locationRequest.setInterval(30 * 1000); 
     locationRequest.setFastestInterval(5 * 1000); 
     LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() 
       .addLocationRequest(locationRequest); 

     builder.setNeedBle(true); 
     //************************** 
     builder.setAlwaysShow(true); //this is the key ingredient 
     //************************** 

     PendingResult<LocationSettingsResult> result = 
       LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.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 could 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 (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; 
       } 
      } 
     }); 
    } 
} 

onActivityResult

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch (requestCode) { 
// Check for the integer request code originally supplied to startResolutionForResult(). 
     case REQUEST_CHECK_SETTINGS: 
      switch (resultCode) { 
       case Activity.RESULT_OK: 
        //startLocationUpdates(); 
        break; 
       case Activity.RESULT_CANCELED: 
        enableGpsSetting(); 
        break; 
      } 
      break; 
    } 
} 

我所做的更改是Outerclass.this到MainActivity.this 我正在擴展FragmentActivity不知道它是否有任何不同。

當代碼作爲指導並且不工作時,它有點奇怪。

+0

您可以跳過的實現,請檢查該[上一頁SO票(http://stackoverflow.com/questions/31235564/locationsettingsrequest-dialog-onactivityresult-skipped)說明如何正確地實現locationsettingsrequest。建設者。我希望這個幫助。 –

+0

對於仍然遇到此問題的人,您需要在連接到Google Play服務後啓動位置設置請求,該服務爲「GoogleApiClient.ConnectionsCallback#onConnected」 – mr5

回答

0

這個問題發生在我身上一次。 (實際上今天!)

我正在測試別的東西,並在我的應用程序中的某個時間點調用此對話框來更改GPS設置。經過多次測試,我已經厭倦了所有時間拒絕許可gps。 (GPS對我所測試的並不重要。)所以我使用了從不按鈕。對話從未出現過。然後當我再次需要他時,他沒有出現。

上線

最終狀態狀態= result.getStatus()

他開始返回代碼8502 - 進入交換機的情況下SETTINGS_CHANGE_UNAVAILABLE。

情況下LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:

如果這是你可以卸載應用程序,並重新安裝的情況下,該對話框更改配置再次出現。

0

使用下面的代碼。

protected void createLocationRequest() { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(LocationServices.API) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .build(); 
    mGoogleApiClient.connect(); 

    LocationRequest mLocationRequest = new LocationRequest(); 
    mLocationRequest.setInterval(1000*5); 
    mLocationRequest.setFastestInterval(1000*2); 
    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 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(
           CurrentActivity.this, 
           REQUEST_CHECK_SETTINGS); 
        } catch (IntentSender.SendIntentException e) { 
         // Ignore the error. 
        } 
        break; 
       case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: 

        break; 
      } 
     } 
    }); 
}