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.
    }
}

相关内容

热门资讯

透视解密"pokem... 透视解密"pokemmo辅助脚本"果然存在有辅助攻略(哔哩哔哩)1、pokemmo辅助脚本公共底牌简...
透视开挂"wepok... 透视开挂"wepoker辅助器安装包定制"好像存在有辅助教程(哔哩哔哩)1、不需要AI权限,帮助你快...
透视有挂"wepok... 透视有挂"wepoker轻量版辅助"都是是有辅助插件(哔哩哔哩)1、进入到wepoker轻量版辅助是...
透视解迷"wepok... 透视解迷"wepoker怎么买辅助"确实真的有辅助器(哔哩哔哩)一、wepoker怎么买辅助游戏安装...
透视必备"wpk软件... 透视必备"wpk软件是正规的吗"果然存在有辅助脚本(哔哩哔哩)1、下载好wpk软件是正规的吗脚本下载...
透视透视"We po... 透视透视"We poker辅助器下载"竟然真的是有辅助脚本(哔哩哔哩)1.We poker辅助器下载...
透视必备"wepok... 透视必备"wepoker透视版下载"都是有辅助器(哔哩哔哩)运wepoker透视版下载辅助工具,进入...
透视分享"聚星ai辅... 透视分享"聚星ai辅助工具下载"好像是有辅助工具(哔哩哔哩)在进入聚星ai辅助工具下载软件靠谱后,参...
透视有挂"hhpok... 透视有挂"hhpoker辅助挂是真的吗"真是真的是有辅助神器(哔哩哔哩)1、该软件可以轻松地帮助玩家...
透视解谜"智星德州插... 透视解谜"智星德州插件最新版本更新内容详解"切实真的有辅助脚本(哔哩哔哩)1、透视解谜"智星德州插件...