Android Gmaps提示用户在MyLocationButton单击时启用GPS



问题:谷歌地图上启用了MyLocationButton,GPS已关闭。当用户单击它时,它只是静默失败。这是一个非常糟糕的用户交互。我希望它提示用户更改GPS设置(就像在谷歌地图上一样(。

似乎我可以重新定义按钮的处理程序,但我喜欢等待用户位置和居中地图部分(并且很乐意避免重写它(。有什么方法可以捕获按钮失败事件吗?

谢谢。

我认为这将通过检查是否启用了 GPS 并在禁用时提醒用户来解决。 我从此链接复制了代码。

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();
}

最新更新