For posterity's sake, I use this for backward compatibility...
public final class OrientationHelper {
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static void setRequestedOrientationSensorPortrait(Activity activity) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
} else {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static void setRequestedOrientationSensorLandscape(Activity activity) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
} else {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
}
usage
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OrientationHelper.setRequestedOrientationSensorPortrait(this);
super.setContentView(R.layout.my_layout);
}