【问题标题】:Android is it possible to check user allow or not allow location permissionAndroid是否可以检查用户允许或不允许位置权限
【发布时间】:2015-10-22 10:26:03
【问题描述】:

我正在处理位置服务,我需要知道如何检查用户单击按钮允许或拒绝权限对话框?或者该应用的定位服务是打开还是关闭?

【问题讨论】:

标签: android location android-location


【解决方案1】:

是的,这很容易做到。请参阅documentation. 下面的代码从文档中复制而来,展示了如何检查用户是否批准或取消了权限请求:

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

【讨论】:

  • 我认为它只是在 6.0 中使用。
猜你喜欢
  • 2021-04-14
  • 2019-04-28
  • 1970-01-01
  • 2010-12-26
  • 2020-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-24
相关资源
最近更新 更多