Search code examples
javaandroidwakelockpowermanager

What vertical bar('|') means in android?


For example:

WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK|
        PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.ON_AFTER_RELEASE, "Alarm");

What does the ' | ' character mean?


More details about the problem:

I'm asking this because when I instantiate the wakelock with only PowerManager.AQUIRE_CAUSES_WAKEUP the program stops working, where as when I use the way above, it works fine.

I'm wondering if the cause of this is because the program ignore the ACQUIRE_CAUSES_WAKEUP tag and it ends up not being used.


Solution

  • In general, that symbol (|) is a bitwise OR. It's used in a lot of different languages and environments outside of Android.

    It's usage is:

    "X|Y : if X or Y is 1, then the result is 1"

    In your specific case it's being used to create a bit field. Besure to review the Android power manager code base here.

    Possible flags for this API are:

    PARTIAL_WAKE_LOCK        = 0x01 
    SCREEN_DIM_WAKE_LOCK     = 0x06
    SCREEN_BRIGHT_WAKE_LOCK  = 0x0a
    FULL_WAKE_LOCK           = 0x1a
    

    These are mutually exclusive (you can only pick one), but you can "OR in" some other flags:

    ON_AFTER_RELEASE         = 0x20000000
    ACQUIRE_CAUSES_WAKEUP    = 0x10000000
    

    So once your code runs it ORs these flags together resulting in:

        0x20000000
      | 0x10000000
      | 0x0000001a
    ---------------
        0x3000001a
    

    I'm asking this because when i instantiate the wakelock with only "PowerManager.AQUIRE_CAUSES_WAKEUP" the program stops working

    That's because you have to pick one of the levels of wake lock (PARTIAL, SCREEN_DIM, SCREEN_BRIGHT, or FULL), you're trying to run with just one of the optional wake lock flags...