PermissionsAndroid
需要本地代码的项目
The following section only applies to projects with native code exposed. If you are using the managed Expo workflow, see the guide on Permissions in the Expo documentation for the appropriate alternative.
PermissionsAndroid
提供对 Android M 新权限模型的访问。所谓的 "normal" 权限,只要出现在 AndroidManifest.xml
中,都是在应用安装时默认授予的。但是,"dangerous" 权限需要对话框提示。你应该使用此模块来获得这些权限。
¥PermissionsAndroid
provides access to Android M's new permissions model. The so-called "normal" permissions are granted by default when the application is installed as long as they appear in AndroidManifest.xml
. However, "dangerous" permissions require a dialog prompt. You should use this module for those permissions.
在 SDK 版本 23 之前的设备上,如果权限出现在清单中,则会自动授予权限,因此 check
应始终生成 true
,request
应始终解析为 PermissionsAndroid.RESULTS.GRANTED
。
¥On devices before SDK version 23, the permissions are automatically granted if they appear in the manifest, so check
should always result to true
and request
should always resolve to PermissionsAndroid.RESULTS.GRANTED
.
如果用户之前关闭了你提示的权限,操作系统将建议你的应用显示需要该权限的理由。可选的 rationale
参数仅在必要时显示对话框提示 - 否则会出现正常的权限提示。
¥If a user has previously turned off a permission that you prompt for, the OS will advise your app to show a rationale for needing the permission. The optional rationale
argument will show a dialog prompt only if necessary - otherwise the normal permission prompt will appear.
示例
¥Example
需要提示用户的权限
¥Permissions that require prompting the user
在 PermissionsAndroid.PERMISSIONS
下可用作常量:
¥Available as constants under PermissionsAndroid.PERMISSIONS
:
-
READ_CALENDAR
:'android.permission.READ_CALENDAR' -
WRITE_CALENDAR
:'android.permission.WRITE_CALENDAR' -
CAMERA
:'android.permission.CAMERA' -
READ_CONTACTS
:'android.permission.READ_CONTACTS' -
WRITE_CONTACTS
:'android.permission.WRITE_CONTACTS' -
GET_ACCOUNTS
:'android.permission.GET_ACCOUNTS' -
ACCESS_FINE_LOCATION
:'android.permission.ACCESS_FINE_LOCATION' -
ACCESS_COARSE_LOCATION
:'android.permission.ACCESS_COARSE_LOCATION' -
ACCESS_BACKGROUND_LOCATION
:'android.permission.ACCESS_BACKGROUND_LOCATION' -
RECORD_AUDIO
:'android.permission.RECORD_AUDIO' -
READ_PHONE_STATE
:'android.permission.READ_PHONE_STATE' -
CALL_PHONE
:'android.permission.CALL_PHONE' -
READ_CALL_LOG
:'android.permission.READ_CALL_LOG' -
WRITE_CALL_LOG
:'android.permission.WRITE_CALL_LOG' -
ADD_VOICEMAIL
:'com.android.voicemail.permission.ADD_VOICEMAIL' -
USE_SIP
:'android.permission.USE_SIP' -
PROCESS_OUTGOING_CALLS
:'android.permission.PROCESS_OUTGOING_CALLS' -
BODY_SENSORS
:'android.permission.BODY_SENSORS' -
SEND_SMS
:'android.permission.SEND_SMS' -
RECEIVE_SMS
:'android.permission.RECEIVE_SMS' -
READ_SMS
:'android.permission.READ_SMS' -
RECEIVE_WAP_PUSH
:'android.permission.RECEIVE_WAP_PUSH' -
RECEIVE_MMS
:'android.permission.RECEIVE_MMS' -
READ_EXTERNAL_STORAGE
:'android.permission.READ_EXTERNAL_STORAGE' -
WRITE_EXTERNAL_STORAGE
:'android.permission.WRITE_EXTERNAL_STORAGE' -
BLUETOOTH_CONNECT
:'android.permission.BLUETOOTH_CONNECT' -
BLUETOOTH_SCAN
:'android.permission.BLUETOOTH_SCAN' -
BLUETOOTH_ADVERTISE
:'android.permission.BLUETOOTH_ADVERTISE' -
ACCESS_MEDIA_LOCATION
:'android.permission.ACCESS_MEDIA_LOCATION' -
ACCEPT_HANDOVER
:'android.permission.ACCEPT_HANDOVER' -
ACTIVITY_RECOGNITION
:'android.permission.ACTIVITY_RECOGNITION' -
ANSWER_PHONE_CALLS
:'android.permission.ANSWER_PHONE_CALLS' -
READ_PHONE_NUMBERS
:'android.permission.READ_PHONE_NUMBERS' -
UWB_RANGING
:'android.permission.UWB_RANGING' -
BODY_SENSORS_BACKGROUND
:'android.permission.BODY_SENSORS_BACKGROUND' -
READ_MEDIA_IMAGES
:'android.permission.READ_MEDIA_IMAGES' -
READ_MEDIA_VIDEO
:'android.permission.READ_MEDIA_VIDEO' -
READ_MEDIA_AUDIO
:'android.permission.READ_MEDIA_AUDIO' -
POST_NOTIFICATIONS
:'android.permission.POST_NOTIFICATIONS' -
NEARBY_WIFI_DEVICES
:'android.permission.NEARBY_WIFI_DEVICES' -
READ_VOICEMAIL
:'com.android.voicemail.permission.READ_VOICEMAIL', -
WRITE_VOICEMAIL
:'com.android.voicemail.permission.WRITE_VOICEMAIL',
请求权限的结果字符串
¥Result strings for requesting permissions
在 PermissionsAndroid.RESULTS
下可用作常量:
¥Available as constants under PermissionsAndroid.RESULTS
:
-
GRANTED
:'granted' -
DENIED
:'denied' -
NEVER_ASK_AGAIN
:'never_ask_again'
参考
¥Reference
方法
¥Methods
check()
static check(permission: Permission): Promise<boolean>;
返回一个解析为布尔值的 promise,以确定是否已授予指定的权限。
¥Returns a promise resolving to a boolean value as to whether the specified permissions has been granted.
参数:
¥Parameters:
名称 | 类型 | 必需的 | 描述 |
---|---|---|---|
permission | string | 是的 | 要检查的权限。 |
request()
static request(
permission: Permission,
rationale?: Rationale,
): Promise<PermissionStatus>;
提示用户启用权限并返回解析为字符串值的 promise(请参阅上面的结果字符串),指示用户是否允许或拒绝该请求,或者不想再次询问。
¥Prompts the user to enable a permission and returns a promise resolving to a string value (see result strings above) indicating whether the user allowed or denied the request or does not want to be asked again.
如果提供了 rationale
,此功能会向操作系统检查是否有必要显示一个对话框来解释为什么需要权限 (https://developer.android.com/training/permissions/requesting.html#explain),然后显示系统权限对话框。
¥If rationale
is provided, this function checks with the OS whether it is necessary to show a dialog explaining why the permission is needed (https://developer.android.com/training/permissions/requesting.html#explain) and then shows the system permission dialog.
参数:
¥Parameters:
名称 | 类型 | 必需的 | 描述 |
---|---|---|---|
permission | string | 是的 | 请求的许可。 |
rationale | object | 不 | 参见下面的 rationale 。 |
理由:
¥Rationale:
名称 | 类型 | 必需的 | 描述 |
---|---|---|---|
title | string | 是的 | 对话框的标题。 |
message | string | 是的 | 对话框的消息。 |
buttonPositive | string | 是的 | 肯定按钮的文本。 |
buttonNegative | string | 不 | 否定按钮的文本。 |
buttonNeutral | string | 不 | 中性按钮的文本。 |
requestMultiple()
static requestMultiple(
permissions: Permission[],
): Promise<{[key in Permission]: PermissionStatus}>;
提示用户在同一对话框中启用多个权限,并返回一个对象,其中权限作为键,字符串作为值(请参阅上面的结果字符串),指示用户是允许还是拒绝该请求,或者不想再次询问。
¥Prompts the user to enable multiple permissions in the same dialog and returns an object with the permissions as keys and strings as values (see result strings above) indicating whether the user allowed or denied the request or does not want to be asked again.
参数:
¥Parameters:
名称 | 类型 | 必需的 | 描述 |
---|---|---|---|
permissions | array | 是的 | 要请求的权限数组。 |