Android动态定位权限

判断是否开启了定位权限

if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED){//未开启定位权限
            //开启定位权限,200是标识码
            ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},200);
        }else{
            //开始定位
            Toast.makeText(MainActivity.this,"已开启定位权限",Toast.LENGTH_LONG).show();
        }

//设置打开

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case
                    200:
                if (grantResults[0]==PackageManager.PERMISSION_GRANTED){
                }else {
                    Toast.makeText(this,"未开启定位权限,请手动到设置去开去权限", Toast.LENGTH_SHORT).show();
                }
                break;
            default:break;
        }
    }

你可能感兴趣的:(Android动态定位权限)