简体   繁体   English

请求许可作为选项

[英]Requesting permission as an option

Hey I am using an API level below 23. 嘿,我使用的是低于23的API级别。

I want to ask users in app if they want to enable read contacts so I have 我想在应用程序中询问用户是否要启用阅读联系人,所以我有

<uses-permission android:name="android.permission.WRITE_CALENDAR" />

I am trying to use this code to ask for permission at run time 我试图使用此代码在运行时请求权限

    private void checkPermissions() {
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_CALENDAR)
            != PackageManager.PERMISSION_GRANTED) {
        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.WRITE_CALENDAR)) {
            Log.d("ABC", "1");
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_CALENDAR},
                    MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        }else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_CALENDAR},
                    MY_PERMISSIONS_REQUEST_READ_CONTACTS);
            Log.d("ABC", "2");
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.d("ABC", "GRANTED");

            } else {
                Log.d("ABC", "Denied");
            }
            return;
        }
    }
}

But if i do not include the permission it automatically declines the request, and if i do it never needs to ask. 但如果我不包含权限,它会自动拒绝请求,如果我这样做,则永远不需要询问。

How can I make it so contacts are not included in the app permissions, but I can ask for them during run time? 我怎样才能这样做,以便联系人不包含在应用程序权限中,但我可以在运行时询问它们? Thanks 谢谢

There is no way to ask for permission that doesn't exist in Android Manifest. 无法请求Android Manifest中不存在的权限。

A good example ----> https://github.com/googlesamples/android-RuntimePermissions 一个很好的例子----> https://github.com/googlesamples/android-RuntimePermissions

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

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