tags:

views:

32

answers:

2

I noticed a lot of Android functions have a parameter that you can pass in that's a bitmask, for different options, like on PendingIntent, you can pass in things like you can call getActivity() with PendingIntent.FLAG_CANCEL_CURRENT|PendingIntent.FLAG_NO_CREATE.

I'm wondering how I can create a function that has a parameter like this?

+1  A: 

They're done manually, simply by defining flags at powers of two. This file uses the left bit-shift operator, but that's not required:

public static final int FLAG_ONE_SHOT = 1<<30;
//...
public static final int FLAG_NO_CREATE = 1<<29;
Matthew Flaschen
+1  A: 
public static final int FLAG_1 = 1<<0; // 0x01
public static final int FLAG_2 = 1<<1; // 0x02
public static final int FLAG_3 = 1<<2; // 0x04
public static final int FLAG_4 = 1<<3; // 0x08

public void myFlagsFunction( int flags ) {
  if ( 0 != ( flags & FLAG_1 ) ) {
    // do stuff
  }
  if ( 0 != ( flags & FLAG_2 ) ) {
    // do stuff
  }
}
drawnonward