2016-04-30 73 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; 
      } 
     } 
    }); 
}