【问题标题】:How I may avoid making spaghetti code while trying to ask user for permissions?在尝试向用户询问权限时如何避免制作意大利面条代码?
【发布时间】:2020-08-13 11:31:14
【问题描述】:

你能解释一下,我应该怎么做?我完全发现了这种废话。因此,我正在尝试制作一个“欢迎屏幕”,其中将要求用户授予某些位置权限。在我删除所有内容并再次开始之前,再次遇到同样的问题 - 当用户已经授予权限时,来自 FusedLocationProviderClient 的代码仍然想检查权限是否被授予(!)有什么我做错了吗?看一些例子:

String[] mPermissions = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
FusedLocationProviderClient mClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome_screen);
    mClient = LocationServices.getFusedLocationProviderClient(this);

    checkPermissions();
}

private void checkPermissions() {
    if (EasyPermissions.hasPermissions(this, mPermissions)) {
        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    Activity#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for Activity#requestPermissions for more details.
            return;
        }
        mClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
                    @Override
                    public void onSuccess(Location location) {
                        //TODO
                    }
                }
        );
    }
}

@Override
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
    if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    Activity#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for Activity#requestPermissions for more details.
        return;
    }
    mClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {

                }
            }
    );
}

@Override
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {}

如何在不同时多次请求权限的情况下使其工作?

【问题讨论】:

    标签: java android permissions


    【解决方案1】:

    我不确定我是否正确理解了这个问题,但根据我的理解:

    如果您不希望它在授予权限后检查权限,您可以创建一个布尔值 h​​asPermission 并在调用该方法进行检查之前添加一个 if 语句。

    if(!hasPermission) checkPermissions();

    在 checkPermissions 方法中,如果检测到权限已经被授予,你只需设置 hasPermission = true ,它就不会再次检查,因为 if 条件将不再为真。

    【讨论】:

    • 如果问题不够清楚,最好要求更多澄清。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多