Activity.requestPermissions与ActivityCompat.requestPermissions [英] Activity.requestPermissions vs ActivityCompat.requestPermissions

查看:1546
本文介绍了Activity.requestPermissions与ActivityCompat.requestPermissions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行时权限对话框显示在Android 6.0或更高版本中,因此在API级别23中添加的Activity.requestPermissions(...)才有意义.

Runtime permission dialog is shown in Android 6.0 or higher, so Activity.requestPermissions(...) which was added in API level 23 makes sense.

但是为什么还要有另一个(ActivityCompat.requestPermissions(...))用于低于Android 6.0的版本?这会在较低版本中显示运行时权限请求对话框吗?

But why is there another one (ActivityCompat.requestPermissions(...)) to be used for below Android 6.0? Does this show runtime permission request dialog in lower versions?

  • 两者之间有什么区别

  • What is the difference between

Activity.requestPermissions(permissions, requestCode)

ActivityCompat.requestPermissions(activity, permissions, requestCode)

  • 我应该使用哪个?

  • Which one should I use?

    推荐答案

    这会在较低版本中显示运行时权限请求对话框吗?

    Does this show runtime permission request dialog in lower versions?

    不.在较低版本上没有这样的对话框.它将仅调用您的onRequestPermissionsResult()方法,以告知您您拥有该权限(根据定义,既然您已经这样做了).

    No. There is no such dialog on lower versions. It will simply call your onRequestPermissionsResult() method to let you know that you hold the permissions (since, by definition, you already do).

    两者之间有什么区别

    What is the difference between

    Activity#requestPermissions()适用于minSdkVersion为23或更高的应用程序,或者适用于开发人员喜欢调用if (Build.VERSION.SDK_INT >= 23)以避免在较旧设备上调用该应用程序的应用程序.

    Activity#requestPermissions() is for apps with a minSdkVersion of 23 or higher, or for apps whose developers like calling if (Build.VERSION.SDK_INT >= 23) to avoid that call on older devices.

    ActivityCompat.requestPermissions()适用于任何应用程序,因为它在所有受支持的API级别(返回到API级别14 IIRC)上都是做正确的事".

    ActivityCompat.requestPermissions() is for any app, as it "does the right thing" on all supported API levels (back to API Level 14 IIRC).

    我应该使用哪个?

    Which one should I use?

    如果您的minSdkVersion为23或更高,请随时使用Activity#requestPermissions().否则,我建议ActivityCompat.requestPermissions().

    If your minSdkVersion is 23 or higher, feel free to use Activity#requestPermissions(). Otherwise, I recommend ActivityCompat.requestPermissions().

    这篇关于Activity.requestPermissions与ActivityCompat.requestPermissions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆