2016年2月28日 星期日

enum or IntDef/StringDef in Android

Some basic information for enum and IntDef/StringDef:


use enum in adnroid is not efficient ,so we use IntDef/StringDef to replace it

How to use IntDef/StringDef:
Annotation explain:

Enumerated Annotations explain:

Now ,show how to use IntDef replace the Enum

if we have a function
input(int i)

it only can accpet the below number

public static final int LOCATION_STATUS_OK = 0;
public static final int LOCATION_STATUS_SERVER_DOWN = 1;
public static final int LOCATION_STATUS_SERVER_INVALID = 2;
public static final int LOCATION_STATUS_UNKNOWN = 3;

how can we limite the function only accept the above:
Method1:,we can use enum

enum State {
       LOCATION_STATUS_OK, LOCATION_STATUS_SERVER_DOWN, LOCATION_STATUS_SERVER_INVALID,LOCATION_STATUS_UNKNOWN
   }
void input(State s){
//do something
}

However, enum in android is not efficient ,we will not use it

Method2:use int

public static final int LOCATION_STATUS_OK = 0;
public static final int LOCATION_STATUS_SERVER_DOWN = 1;
public static final int LOCATION_STATUS_SERVER_INVALID = 2;
public static final int LOCATION_STATUS_UNKNOWN = 3;

void input(int state){
//do something
}

however, we can’t not limited user only input the above number,we need to write more function to prevent the use input other number

Method3:

use IntDef

@IntDef(flag = true, value = {LOCATION_STATUS_OK, LOCATION_STATUS_SERVER_DOWN,LOCATION_STATUS_SERVER_INVALID,LOCATION_STATUS_UNKNOWN})
   @Retention(RetentionPolicy.SOURCE)
   public @interface State {}
public static final int LOCATION_STATUS_OK = 0;
public static final int LOCATION_STATUS_SERVER_DOWN = 1;
public static final int LOCATION_STATUS_SERVER_INVALID = 2;
public static final int LOCATION_STATUS_UNKNOWN = 3;

void input(@Stateint state){
//do something
}



Some information of RetentionPolicy

RetentionPolicy.SOURCE: Discard during the compile. These annotations don't make any sense after the compile has completed, so they aren't written to the bytecode.
Example: @Override, @SuppressWarnings

RetentionPolicy.CLASS: Discard during class load. Useful when doing bytecode-level post-processing. Somewhat surprisingly, this is the default.


RetentionPolicy.RUNTIME: Do not discard. The annotation should be available for reflection at runtime. Example: @Deprecated


example to use

step1:define the state code
@IntDef(flag = true, value = {LOCATION_STATUS_OK, LOCATION_STATUS_SERVER_DOWN, LOCATION_STATUS_SERVER_INVALID, LOCATION_STATUS_UNKNOWN})
   @Retention(RetentionPolicy.SOURCE)
   public @interface LocationStates {
   }
   public static final int LOCATION_STATUS_OK = 0;
   public static final int LOCATION_STATUS_SERVER_DOWN = 1;
   public static final int LOCATION_STATUS_SERVER_INVALID = 2;
   public static final int LOCATION_STATUS_UNKNOWN = 3;

step2,define the function to save the state by sharedPreference

private void setLocationState(@LocationStates int locationstate){
       SharedPreferences sp = mContext.getSharedPreferences(mContext.getString(R.string.pref_location_state_key), Context.MODE_PRIVATE);
       SharedPreferences.Editor editor = sp.edit();
       editor.putInt(mContext.getString(R.string.pref_location_state_key), locationstate);
       editor.commit();
   }


step3 call the function on different location in the app