【问题标题】:How to prompt user to turn on location?如何提示用户开启定位?
【发布时间】:2016-08-09 10:56:41
【问题描述】:

我有一个应用程序,当我们单击一个按钮时,该应用程序会启动带有搜索查询的谷歌地图。但我需要打开位置以提供准确的结果。有可能吗?

【问题讨论】:

标签: android google-maps geolocation google-maps-android-api-2


【解决方案1】:

我了解您的问题 这是应该添加到 onCreate 方法中的代码

LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
            !lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        // Build the alert dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Location Services Not Active");
        builder.setMessage("Please enable Location Services and GPS");
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                // Show location settings when the user acknowledges the alert dialog
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        });
        Dialog alertDialog = builder.create();
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.show();
    }

【讨论】:

    【解决方案2】:

    您应该在 AndroidManifest.xml 中添加 permission

    关于 Android 权限的一般信息here

    注意 Android 6 权限机制已更改 (it became iOS like)。

    【讨论】:

      猜你喜欢
      • 2019-11-11
      • 2011-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-20
      相关资源
      最近更新 更多