简体   繁体   English

我的应用程序第一次不询问棉花糖的运行时权限吗?

[英]My app Does not ask at the first time for the runtime permission for the marshmallow?

I making an app for the food delivery. 我为送餐制作了一个应用程序。 In my app when the user use android phone version of marshmallow then my app does not ask at the first time for the permission of the read the sms. 在我的应用程序中,当用户使用Android手机版本的棉花糖时,我的应用程序不会在第一时间要求读取短信的许可。 but when user open the app next time then my app ask for the permission. 但是当用户下次打开应用程序时,我的应用程序会请求许可。 I don't know what is the problem. 我不知道是什么问题。 I want that when the user open the app first time permission should ask.instead of the second time. 我希望当用户第一次打开应用程序时,应该询问权限,而不是第二次。

This is my method for asking the permission I am calling this method in my onCreate() method of my Activity: 这是我请求权限的方法,我在Activity的onCreate()方法中调用此方法:

 private void permissionForMarshMallow() {

        int permissionCheck = ContextCompat.checkSelfPermission(LoginActivity.this,
                Manifest.permission.READ_SMS);

        if (ContextCompat.checkSelfPermission(LoginActivity.this,
                Manifest.permission.READ_SMS)
                != PackageManager.PERMISSION_GRANTED) {


            if (ActivityCompat.shouldShowRequestPermissionRationale(LoginActivity.this,
                    Manifest.permission.READ_SMS)) {

                Log.e("permission...granted", "permission granted............");


            } else {



                ActivityCompat.requestPermissions(LoginActivity.this,
                        new String[]{Manifest.permission.READ_SMS},
                        REQUEST_CODE_ASK_SINGLE_PERMISSION);


            }
        }
    } 

and this is the override method onRequestPermissionsResult() : 这是重写方法onRequestPermissionsResult()

 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {


        switch (requestCode) {

            case REQUEST_CODE_ASK_SINGLE_PERMISSION:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                } else {


                    // Toast.makeText(LoginActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }


    }

Can anyone tell me How can I make the app that it ask for the permission at the first time not second time? 谁能告诉我如何使应用程序在第一次而不是第二次请求许可?

There is a logic flaw in your code. 您的代码中存在逻辑缺陷。 This is how your code runs: 这是您的代码运行方式:

  1. Check if we have the permission. 检查我们是否有权限。
  2. If we DO NOT have the permission: 如果我们没有许可:
    • Should we ask permission? 我们应该征求许可吗?
      • If yes, log "permission granted" 如果是,则记录“已授予权限”
      • If not, then request permission 如果没有,则请求权限

So basically, you are requesting permission when you shouldn't. 因此,基本上,您是在不需要的时候请求许可。 In a case where you should ask for permission, instead of asking permission you just log "permission granted". 在您应该请求许可的情况下,只需记录“已授予许可”,而不是请求许可。 You just have to change your code a little bit: 您只需要稍微更改一下代码即可:

 private void permissionForMarshmallow() {

        int permissionCheck = ContextCompat.checkSelfPermission(LoginActivity.this, Manifest.permission.READ_SMS);

        if (ContextCompat.checkSelfPermission(LoginActivity.this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(LoginActivity.this, new String[]{Manifest.permission.READ_SMS}, REQUEST_CODE_ASK_SINGLE_PERMISSION);

        }
        else {
            // user probably checked "never ask again" - we should not ask for permission.
        }
    }
} 

Looks like at first start shouldShowRequestPermissionRationale is true and you just create a log entry. 看起来像第一次启动时,ShowShowRequestPermissionRationale为true,您只需创建一个日志条目。 You wrote it in your log but in this case the permission is not granted yet. 您已将其写入日志中,但在这种情况下,尚未授予该权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM