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