Class Permissions

java.lang.Object
com.codename1.system.Permissions

public final class Permissions extends Object

Checks and requests runtime permissions in one place.

This class declares nothing. A permission reaches the Android manifest or the iOS Info.plist only because the build detected a feature class that needs it (the camera, location, contacts and so on). Asking for a permission no feature declared answers DENIED, and the log names the reason.

#request(String, String) blocks until the user answers, so call it from the EDT (it uses invokeAndBlock where needed) or from a background thread.

if (Permissions.check(Permissions.CAMERA) != Permissions.GRANTED) {
    int status = Permissions.request(Permissions.CAMERA, "To scan receipts");
    Log.p("Camera permission: " + status);
}
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Bluetooth devices.
    static final String
    The calendar.
    static final String
    The camera.
    static final String
    The address book.
    static final int
    The permission is denied, or was never declared.
    static final int
    The user denied it and the platform no longer shows the prompt; only the system settings can grant it now.
    static final int
    The permission is granted.
    static final int
    Granted with restrictions, such as limited photo access or approximate location.
    static final String
    Location while the application is in use.
    static final String
    Location in the background.
    static final String
    The microphone.
    static final int
    The user has not been asked yet.
    static final String
    Posting notifications.
    static final String
    The photo library.
    static final int
    The platform cannot tell, or has no such permission.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    check(String permission)
    The current status of a permission.
    static boolean
    isGranted(String permission)
    True when the permission is granted, fully or limited.
    static int
    request(String permission, String reason)
    Asks the user for a permission when it is not granted yet, and blocks until they answer.
    static void
    setImplementation(com.codename1.impl.CodenameOneImplementation impl)
    Invoked internally from Display, this method is for internal use only

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • setImplementation

      public static void setImplementation(com.codename1.impl.CodenameOneImplementation impl)

      Invoked internally from Display, this method is for internal use only

      Parameters
      • impl: implementation instance
    • check

      public static int check(String permission)

      The current status of a permission. Never prompts.

      Bluetooth is answered by com.codename1.bluetooth.Bluetooth#hasPermission(BluetoothPermission), and the platform is asked only to tell a refusal from a question not asked yet. Contacts are not answered by Display#isContactsPermissionGranted() here: on iOS that call prompts.

      Parameters
      • permission: one of the permission names of this class
      Returns

      one of the status constants

    • request

      public static int request(String permission, String reason)

      Asks the user for a permission when it is not granted yet, and blocks until they answer.

      Contacts go through Display#isContactsPermissionGranted(), notifications through Display#requestNotificationPermission(NotificationPermissionCallback) and Bluetooth through com.codename1.bluetooth.Bluetooth#requestPermissions(BluetoothPermission...) (scan and connect), so the prompt is the one those APIs show.

      Parameters
      • permission: one of the permission names of this class
      • reason: the explanation shown where the platform asks for one
      Returns

      the status after the request

    • isGranted

      public static boolean isGranted(String permission)

      True when the permission is granted, fully or limited.

      Parameters
      • permission: one of the permission names of this class
      Returns

      true when granted