2017-02-05 33 views
0

問題:在Google地圖上啓用了MyLocationButton,並且GPS已關閉。當用戶點擊它時,它就會失敗。這是一個非常糟糕的用戶交互。我希望它提示用戶更改GPS設置(就像它在谷歌地圖上的實際情況一樣)。Android Gmaps提示用戶在MyLocationButton上啓用GPS點擊

看來我可以重新定義按鈕的處理程序,但我喜歡等待用戶位置和居中映射部分(並很樂意避免重寫它)。有什麼辦法可以捕捉按鈕失敗事件嗎?

感謝。

+0

參見[這個問題](http://stackoverflow.com/questions/16262946/how-to-enable-gps-in-android) –

+0

@NicolasMaltais謝謝。然而,這不是問題的關鍵。我知道如何提示用戶輸入gps。我想要的是趕上MyLocationButton發送該請求的失敗。 – ClonedOne

+0

用[此方法]檢查gps是否啓用後(http://stackoverflow.com/questions/843675/how-do-i-find-out-if-the-gps-of-an-android-device -is-enabled)工作? –

回答

-1

我認爲這將通過檢查GPS是否啓用來解決,並警告用戶是否被禁用。我複製了this link的代碼。

final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
     buildAlertMessageNoGps(); 
    } 

private void buildAlertMessageNoGps() { 
    final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setMessage("Your GPS seems to be disabled, do you want to enable it?") 
      .setCancelable(false) 
      .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
       public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) { 
        startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 
       } 
      }) 
      .setNegativeButton("No", new DialogInterface.OnClickListener() { 
       public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) { 
        dialog.cancel(); 
       } 
      }); 
    final AlertDialog alert = builder.create(); 
    alert.show(); 
} 
+0

謝謝,但我的問題不是如何檢查GPS是否處於活動狀態,而是如何響應MyLocationButton失敗。對於我能理解的這段代碼並沒有這樣做,相反它只是在活動生命週期中調用GPS可用性之後才進行檢查。請糾正我,如果我錯了 – ClonedOne

+0

什麼時候MyLocationButton失敗?當GPS被禁用時。 –