人造人。提示访问设备的位置



在我的旧Android(三星S3 mini)中,没有必要做任何东西来允许我的应用程序使用手机的位置(GPS)。现在我正在使用带有Android 4的LG G6.0,并且从未使用过GPS。我可以看到在Waze等应用程序中,有一个提示"允许Waze访问此设备的位置? 我想在启动我的应用程序之前,我必须做类似的事情来触发此选项。任何人都知道如何做到这一点。我不知道如何问谷歌。提前谢谢。

在 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

最新更新