Android可以检查用户是否允许或不允许位置许可

时间:2015-10-22 10:26:04

标签: android location android-location

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

enter image description here

1 个答案:

答案 0 :(得分:0)

是的,可以轻松完成。请参阅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
    }
}