首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法设置权限ACCESS_FINE_LOCATION

无法设置权限ACCESS_FINE_LOCATION
EN

Stack Overflow用户
提问于 2016-03-05 19:57:43
回答 1查看 1.1K关注 0票数 4

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

清单:

代码语言:javascript
复制
<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"/>

MainActivity:

代码语言:javascript
复制
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
Log.d("Check if permission", "permission is: " + permissionCheck);

Logd返回permission is: -1

我做得这么糟糕是什么原因?

使用Android,target-sdk是23

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-05 20:06:09

checkSelfPermission()将检查用户是否已授予权限。如果检查失败(在本例中为-1),则需要使用requestPermissions()向用户请求权限。

对此有一个很好的解释,并举例说明了如何在android文档(http://developer.android.com/training/permissions/requesting.html#perm-request)中检查、通知和请求权限。

如果链接死了,下面是它们的代码:

代码语言:javascript
复制
// 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 Developer Docs)

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35819132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档