Android-检查用户的位置权限选择
创始人
2024-08-19 19:00:48
0

使用运行时权限检查,可以在用户选择完位置权限之后检查用户的选择并作出相应处理。代码示例:

// check location permission
if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

    // Location permission is granted
     // Do your stuff here

} else {

    // Location permission is not granted
    // Ask for the permission
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
            MY_PERMISSIONS_REQUEST_LOCATION);

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

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

                // permission was granted
                // Do your stuff here

            } else {

                // permission denied
                // Do your stuff here

            }
            return;
        }

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

相关内容

热门资讯

黑科技辅助!wepoke智能a... 黑科技辅助!wepoke智能ai辅助工具,(扑克之城)好像是真的有挂,攻略教程(有挂方法)是一款可以...
黑科技辅助!wepoke辅助插... 黑科技辅助!wepoke辅助插件安装,(菠萝德州软件)原先是真的有挂,黑科技教程(详细教程);菠萝德...
黑科技辅助!wepoke智能a... 黑科技辅助!wepoke智能ai辅助工具,(德州ai盈利)一直是有挂,AI教程(有挂讲解);德州ai...
黑科技辅助!wepoke透明黑... 黑科技辅助!wepoke透明黑科技,(AAPoKer模拟器)往昔是有挂,科技教程(真的有挂);AAP...
黑科技辅助!wepoke辅助插... 黑科技辅助!wepoke辅助插件安装,(微扑克机器人)总是存在有挂,揭秘教程(果真有挂);微扑克机器...
黑科技辅助!wepoke智能a... 黑科技辅助!wepoke智能ai辅助工具,(德扑之星代打)都是真的有挂,必胜教程(今日头条),德扑之...
黑科技辅助!wepoke智能a... 黑科技辅助!wepoke智能ai辅助工具,(AAPoKerai机器人)真是是有挂,力荐教程(有挂辅助...
黑科技辅助!wepoke有挂真... 黑科技辅助!wepoke有挂真的有挂,(红龙扑克机制)原来有挂,高科技教程(有挂教程);亲,有的,a...
黑科技辅助!wepoke辅助插... 黑科技辅助!wepoke辅助插件,(WePoKe黑科技)先前真的是有挂,2025新版教程(发现有挂)...
黑科技辅助!wepoke智能a... 黑科技辅助!wepoke智能ai辅助工具,(悟空黑桃a德州)先前是有挂,解说技巧(有挂秘笈);1、让...