2015-04-20 9 views
14

GPS tabanlı uygulamalarda, kullanıcının GPS özelliğini etkinleştirmesi önemlidir. Eğer değilse, genellikle "kullanıcısının bu işlevselliği kullanabilmesi için ayarlarından GPS'ini etkinleştirmesi gerektiğini belirten bir iletişim kutusu göstereceğiz".Android: Kullanıcıyı "google maps" uygulamasında olduğu gibi ayarlar ekranına yönlendirmeden GPS'yi etkinleştirebilir miyim?

Kullanıcı OK (Tamam) düğmesine bastığında Ayarlar sayfasına yönlendirilecek, bu çözümden hoşlanmıyorum çünkü kullanıcıyı uygulama bağlamından ayarlara getiriyor.

"google maps" uygulamasının daha iyi bir çözümü olduğunu farkettim, bu da bir GPS özelliği gerektiğinde düzgün bir iletişim kutusu göstermektir. Kullanıcının "OK" seçimi üzerine GPS, ayarlarına doğrudan ayarlarında herhangi bir yeniden yönlendirme yapılmadan etkinleştirilecektir.

Kullanıcıyı "google maps" uygulamasında olduğu gibi ayarları ekrana yönlendirmeden GPS'yi etkinleştirebilir miyim? Aşağıdaki resim

çıkış:

Neat Dialog

+0

Doğru mu geldin? @ A.Alqadomi –

+0

@AndreHoffmann, yeni projemizde iş arkadaşım tarafından zaten yapıldı. Gizli, Google’dan yeni Google’ı kullanmaktır. Bu, Google’ın "google oyun hizmetleri" nden elde edilmesine dayanmaktadır. Geri kalanınız açık –

cevap

20

İhtiyacınız bu özelliği sağlamak için:

  • İlk (en azından) oyun hizmetlerinin versiyonu 7,0

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

Kodunuzdaki böyle
  • İkinci şey (benim onCreate içinde olduğunu):

-

// 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); 

Sonra geri arama oluşturun: Sonra

// 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; 
     } 
    } 
}; 

Ve Son olarak, onActivityResult içinde şu vardı:

/** 
* 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

Cevabı onaylamadım, daha sonra deneyeceğim. Yardım için teşekkürler –

+12

Google oyun hizmetleri bir kütüphane kütüphanesidir. Yalnızca konum api'yi dahil etmek için, 'compile 'com.google.android.gms: play-services-location: 8.1.0' ' –

+0

harika kullanın. Teşekkürler. benim için çok zaman kazandı. – hybrid

İlgili konular