onConfigurationChanged

onConfigurationChanged

我已经设置了一个 Activity 来处理配置更改,并且它可以工作,这意味着当方向更改时会调用onConfigurationChanged()

该 Activity 具有一个按钮,用于显式更改方向。单击后,它称为setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT)

然后,将方向设置为不可撤销,并且不再调用onConfigurationChanged()

当用户在不松开onConfigurationChanged()回调的情况下单击按钮时,如何更改方向?

最佳答案

这就是我解决的方法。我知道它正在重新发明轮子,但是它满足了我的要求,我没有找到使用标准sdk工具处理此问题的合适方法。

首先,创建一个OrientationManager类,以监听方向变化

public class OrientationManager extends OrientationEventListener{
private static final String TAG = OrientationManager.class.getName();

private int previousAngle;
private int previousOrientation;
private Context context;
private OrientationChangeListener orientationChangeListener;
private static OrientationManager instance;
private OrientationManager(Context context) {
    super(context);
    this.context = context;
}

public static OrientationManager  getInstance(Context context){
    if (instance == null){
        instance = new OrientationManager(context);
    }
    return instance;
}

public int getOrientation(){
    return previousOrientation;
}

public void setOrientation(int orientation){
    this.previousOrientation = orientation;
}


@Override
public void onOrientationChanged(int orientation) {
    if (orientation == -1)
        return;
    if(previousOrientation == 0){
        previousOrientation = context.getResources().getConfiguration().orientation;
        if (orientationChangeListener != null){
            orientationChangeListener.onOrientationChanged(previousOrientation);
        }
    }
    if (previousOrientation == Configuration.ORIENTATION_LANDSCAPE &&
            ((previousAngle > 10 && orientation <= 10) ||
            (previousAngle < 350 && previousAngle > 270 && orientation >= 350)) ){
        if (orientationChangeListener != null){
            orientationChangeListener.onOrientationChanged(Configuration.ORIENTATION_PORTRAIT);
        }
        previousOrientation = Configuration.ORIENTATION_PORTRAIT;
    }

    if (previousOrientation == Configuration.ORIENTATION_PORTRAIT &&
            ((previousAngle <90 && orientation >= 90 && orientation <270) ||
            (previousAngle > 280 && orientation <= 280 && orientation > 180))   ){
        if (orientationChangeListener != null){
            orientationChangeListener.onOrientationChanged(Configuration.ORIENTATION_LANDSCAPE);
        }
        previousOrientation = Configuration.ORIENTATION_LANDSCAPE;
    }
    previousAngle = orientation;
}

public void setOrientationChangedListener(OrientationChangeListener l){
    this.orientationChangeListener = l;
}

public interface OrientationChangeListener{
    public void onOrientationChanged(int newOrientation);
}
}

然后在您的 Activity 中实现OrientationChangeListener并覆盖onOrientationChanged():
public class MyActivity extends Activity implements OrientationChangeListener{

    private OrientationManager orientationManager;

    @Override
    public void onCreate(Bundle b){
        orientationManager = OrientationManager.getInstance(this);
        orientationManager.setOrientationChangedListener(this);

    }

@Override
public void onOrientationChanged(int newOrientation) {
    orientation = newOrientation;
    if (newOrientation == Configuration.ORIENTATION_LANDSCAPE){
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        setLandscapeConfig();
    }else{
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setPortraitConfig();
    }
}

因此,我不再使用onConfigurationChanged,而是将以下行保留在 list 中:
android:configChanges =“orientation | screenSize”

关于android - 一旦使用setRequestedConfiguration,则不会调用onConfigurationChanged,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13073500/

10-09 00:53