Udostępnij za pośrednictwem


Activity.RequestPermissions(String[], Int32) Method

Definition

Requests permissions to be granted to this application.

[Android.Runtime.Register("requestPermissions", "([Ljava/lang/String;I)V", "", ApiSince=23)]
public void RequestPermissions (string[] permissions, int requestCode);
[<Android.Runtime.Register("requestPermissions", "([Ljava/lang/String;I)V", "", ApiSince=23)>]
member this.RequestPermissions : string[] * int -> unit

Parameters

permissions
String[]

The requested permissions. Must me non-null and not empty.

requestCode
Int32

Application specific request code to match with a result reported to #onRequestPermissionsResult(int, String[], int[]). Should be >= 0.

Attributes

Remarks

Requests permissions to be granted to this application. These permissions must be requested in your manifest, they should not be granted to your app, and they should have protection level android.content.pm.PermissionInfo#PROTECTION_DANGEROUS dangerous, regardless whether they are declared by the platform or a third-party app.

Normal permissions android.content.pm.PermissionInfo#PROTECTION_NORMAL are granted at install time if requested in the manifest. Signature permissions android.content.pm.PermissionInfo#PROTECTION_SIGNATURE are granted at install time if requested in the manifest and the signature of your app matches the signature of the app declaring the permissions.

Call #shouldShowRequestPermissionRationale(String) before calling this API to check if the system recommends to show a rationale UI before asking for a permission.

If your app does not have the requested permissions the user will be presented with UI for accepting them. After the user has accepted or rejected the requested permissions you will receive a callback on #onRequestPermissionsResult(int, String[], int[]) reporting whether the permissions were granted or not.

Note that requesting a permission does not guarantee it will be granted and your app should be able to run without having this permission.

This method may start an activity allowing the user to choose which permissions to grant and which to reject. Hence, you should be prepared that your activity may be paused and resumed. Further, granting some permissions may require a restart of you application. In such a case, the system will recreate the activity stack before delivering the result to #onRequestPermissionsResult(int, String[], int[]).

When checking whether you have a permission you should use #checkSelfPermission(String).

You cannot request a permission if your activity sets android.R.styleable#AndroidManifestActivity_noHistory noHistory to true because in this case the activity would not receive result callbacks including #onRequestPermissionsResult(int, String[], int[]).

The RuntimePermissions sample apps demonstrate how to use this method to request permissions at run time.

Java documentation for android.app.Activity.requestPermissions(java.lang.String[], int).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to