android - 如何从 OrientationEventListener 获取 boolean 类型的返回值?

标签 android orientation

我在 Android Studio 中有一个问题要解决,但我是新手。我必须创建一个变量,该变量在调用 onResume() 方法时一直递增,但如果方向发生变化,则该变量不得递增。

我想在 onResume() 方法中使用 if-else 语句来解决这个问题(如果 OrientationEventListener 返回 false,则变量会递增,但不生效方向改变时的变量(真)),并用 toasts 写出变量的值。但是,我不知道如何从中获取 bool 类型的返回值,即使我搜索了几个小时的答案。 有类似的问题,但我无法成功实现他们的想法。如果有帮助,这是我的代码:

public class MainActivity extends Activity {
    public static final String MY_TAG = "tagged";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void mOrientationListener = new OrientationEventListener(this SensorManager.SENSOR_DELAY_NORMAL) {
        @Override
        public void onOrientationChanged(int orientation) {

        }
    };

    public void onResume() {
        super.onResume();
        setContentView(R.layout.activity_main);
        int sum = 0;
        int ak = int onOrientationChanged;
        if ( != 0) {
            Log.i(MY_TAG, "onResume");
            tToast("onResume:");
            sum++;
            nToast(sum);
        }else {
            nToast(sum);
        }
    }
    private void tToast(String s) {
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, s, duration);
        toast.show();
    }
    private void nToast(Integer a) {
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, a, duration);
        toast.show();
    }
}

最佳答案

OrientationEventListener 在您需要知道设备旋转的精确方向(以度为单位,从 0 到 359)时使用。我假设您是在谈论横向/纵向意义上的方向,因此 OrientationEventListener 并不是解决此问题的正确方法。

我认为最好的办法是每次检查 onCreate() 中的方向,并将其与之前的方向进行比较以设置标志 didChangeOrientation 或类似的东西,然后你可以在你的 onResume() 操作中使用这个标志。

private int orientation;
private boolean didChangeOrientation;

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("orientation", orientation);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    orientation = getResources().getConfiguration().orientation;        

    if (savedInstanceState != null) {
        int previousOrientation = savedInstanceState.getInt("orientation");
        didChangeOrientation = (orientation != previousOrientation);
    }
    else {
        didChangeOrientation = false;
    }

    ...
}

@Override
public void onResume() {
    super.onResume();

    if (!didChangeOrientation) {
        // your code here
    }
}

关于android - 如何从 OrientationEventListener 获取 boolean 类型的返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47019810/

相关文章:

android - 保存状态异常

android - 在 Android 4.4.2 上以编程方式切换移动数据

android - Constraintlayout 中 recyclerview 的行项目折叠而不是采用父宽度

ipad - 将 iPad 从横向旋转到纵向时页面向左移动

android - 改变方向时不要改变背景

javascript - 窗口在方向更改时调整内容大小之前的触发功能?

android - 使用MediaPlayer进行Android流式传输:错误(1,-1004)和3GPP视频

android - 当我开发Android应用程序时,如何检测dolby是打开还是关闭?

android - ActivityInfo 设置屏幕方向

ios - 如何以编程方式将 UIViewController 的方向更改为横向?