【问题标题】:Android. Prompt access device's location安卓。提示访问设备的位置
【发布时间】:2016-10-15 18:18:36
【问题描述】:

在我的旧 Android (Samsung S3 mini) 中,不需要做任何事情来让我的应用程序使用我手机的位置 (GPS)。 现在我正在使用带有 Android 6.0 的 LG G4,它从未使用过 GPS。 我可以看到在 waze 之类的应用程序中,有一个提示“允许 Waze 访问此设备的位置?”。我想在启动我的应用程序之前我必须做一些类似的事情来触发这个选项。 任何人都知道该怎么做。我不知道如何问谷歌。 提前致谢。

【问题讨论】:

标签: android android-gps


【解决方案1】:

在 android 6.0 上,您必须在运行时请求一些权限。这里解释一下https://developer.android.com/training/permissions/requesting.html

从 Android 6.0(API 级别 23)开始,用户授予以下权限 应用程序运行时的应用程序,而不是安装应用程序时的应用程序。

为了在运行时请求权限,你应该这样做(在你想使用 GPS 之前):

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
    // Check Permissions Now
    private static final int REQUEST_LOCATION = 2;

    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.ACCESS_FINE_LOCATION)) {
        // Display UI and wait for user interaction
    } else {
        ActivityCompat.requestPermissions(
              this, new String[]{Manifest.permission.LOCATION_FINE},
    ACCESS_FINE_LOCATION);
    }
} else {
    // permission has been granted, continue as usual
    Location myLocation =
        LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}

这里有一个 GPS 示例https://developers.google.com/android/guides/permissions

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多