2015-04-20 13 views
14

Nelle applicazioni basate su GPS, è importante che l'utente abiliti il ​​suo GPS. In caso contrario, di solito mostreremmo una finestra di dialogo in cui si afferma che l'utente "dovrebbe abilitare il proprio GPS dalle impostazioni per poter utilizzare questa funzionalità".Android: Posso abilitare il GPS senza reindirizzare l'utente alla schermata delle impostazioni come nell'app "google maps"

Quando l'utente preme OK, verrà reindirizzato alla pagina Impostazioni, non mi piace questa soluzione poiché toglie l'utente dal contesto dell'applicazione alle impostazioni.

Ho notato che l'applicazione "google maps" ha una soluzione migliore, che è quella di mostrare una finestra di dialogo pulita quando è necessaria una funzione GPS. Alla selezione dell'utente "OK" il GPS sarà abilitato direttamente senza alcun reindirizzamento alle impostazioni.

Posso abilitare il GPS senza reindirizzare l'utente alla schermata delle impostazioni come nell'app "google maps"?

cassa l'immagine qui sotto:

Neat Dialog

+0

Hai maneggiato per venire bene? @ A.Alqadomi –

+0

@AndreHoffmann è stato già fatto dal mio collega nel nostro nuovo progetto. il segreto è utilizzare la nuova API di Google, basata sull'ottenere la posizione da "servizi di google play". il resto è semplice –

risposta

20

per avere quella funzionalità è necessario:

  • Prima (almeno) la versione 7.0 di servizi di gioco

compile 'com.google.android.gms:play-services:7.0.0'

  • Seconda qualcosa di simile nel codice (che ho avuto nel mio onCreate):

-

// Check the location settings of the user and create the callback to react to the different possibilities 
LocationSettingsRequest.Builder locationSettingsRequestBuilder = new LocationSettingsRequest.Builder() 
       .addLocationRequest(mLocationRequest); 
PendingResult<LocationSettingsResult> result = 
       LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, locationSettingsRequestBuilder.build()); 
result.setResultCallback(mResultCallbackFromSettings); 

e quindi creare il callback:

// The callback for the management of the user settings regarding location 
private ResultCallback<LocationSettingsResult> mResultCallbackFromSettings = new ResultCallback<LocationSettingsResult>() { 
    @Override 
    public void onResult(LocationSettingsResult result) { 
     final Status status = result.getStatus(); 
     //final LocationSettingsStates 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(
          MapActivity.this, 
          REQUEST_CHECK_SETTINGS); 
       } catch (IntentSender.SendIntentException e) { 
        // Ignore the error. 
       } 
       break; 
      case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: 
       Log.e(TAG, "Settings change unavailable. We have no way to fix the settings so we won't show the dialog."); 
       break; 
     } 
    } 
}; 

E poi, infine, in onActivityResult ho avuto il seguente:

/** 
* Used to check the result of the check of the user location settings 
* 
* @param requestCode code of the request made 
* @param resultCode code of the result of that request 
* @param intent intent with further information 
*/ 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    //final LocationSettingsStates states = LocationSettingsStates.fromIntent(intent); 
    switch (requestCode) { 
     case REQUEST_CHECK_SETTINGS: 
      switch (resultCode) { 
       case Activity.RESULT_OK: 
        // All required changes were successfully made 
        if (mGoogleApiClient.isConnected() && userMarker == null) { 
         startLocationUpdates(); 
        } 
        break; 
       case Activity.RESULT_CANCELED: 
        // The user was asked to change settings, but chose not to 
        break; 
       default: 
        break; 
      } 
      break; 
    } 
} 
+0

Non ho ancora confermato la risposta proverò più tardi. Grazie per l'aiuto –

+12

Google Play Services è una libreria huuge. Per includere solo la posizione API, usa 'compile 'com.google.android.gms: play-services-location: 8.1.0'' –

+0

impressionante. Grazie. ho risparmiato un sacco di tempo per me. – hybrid

Problemi correlati