如何使用@ ActivityInfo.ScreenOrientation

Alt*_*yyr 7 java android annotations

我尝试创建一个方法,返回我的屏幕方向依赖于设备是手持设备或平板电脑.

public int getScreenOrientation(boolean isTablet){
    if(isTablet){
        return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
    } else {
        return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当我使用时,setRequestedOrientation(getScreenOrientation));我得到一个lint-error Must be one of: ActivityInfo.SCREEN_ORIENTATION_.........,我可以抑制它,但这看起来不像干净的代码.

所以我发现,它getRequestedOrientation使用@ActivityInfo.ScreenOrientationAnnotation.所以我试着自己使用它:

@ActivityInfo.ScreenOrientation
public int getScreenOrientation(boolean isTablet){
    .
    .
    .
}
Run Code Online (Sandbox Code Playgroud)

但IDE给出了一个错误,指出@ActivityInfo.ScreenOrientation无法找到Annotation .但它在官方-android-source中被公开.

ecl*_*cle 9

将以下注释放在恼人的语句上方,其中触发@IntDef和@StringDef注释的魔术常量检查,例如:

//noinspection ResourceType
setRequestOrientation(lock);
Run Code Online (Sandbox Code Playgroud)