gpt4 book ai didi

android - 无法设置权限 ACCESS_FINE_LOCATION

转载 作者:太空宇宙 更新时间:2023-11-03 13:16:00 26 4
gpt4 key购买 nike

我是 android 开发的新手,现在我很难获取当前用户位置。

list :

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

主要 Activity :

int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
Log.d("Check if permission", "permission is: " + permissionCheck);

Logd 返回权限为:-1

我做错了什么?

使用Android Studio,target-sdk为23

最佳答案

checkSelfPermission() 将检查用户是否已授予权限。如果检查失败(在您的情况下为 -1),则您需要使用 requestPermissions()

向用户请求许可

在 android 文档 ( http://developer.android.com/training/permissions/requesting.html#perm-request ) 中有一个很好的解释,以及如何检查、通知和请求权限的示例

万一链接失效,下面是他们的代码:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {

// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {

// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.

} else {

// No explanation needed, we can request the permission.

ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);

// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}

(来源:Android 开发者文档)

关于android - 无法设置权限 ACCESS_FINE_LOCATION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35819132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com