如何检测屏幕旋转?

17

有没有可能检测屏幕的旋转?我的意思是,只检测旋转,而不是从另一个活动初始化的活动?

对于这个问题,onXxx方法似乎并没有什么用处。我已经尝试从starting Intent添加/删除flag(删除似乎没有反应,在旋转时标志还在),并在清单中为活动添加 android:configChanges="orientation" 属性,但是 onConfigurationChanged 方法似乎每隔一次旋转就会被调用...很奇怪。

我想我可能遗漏了什么......但在其他相关帖子中也没有找到清晰的解决方案。

有什么想法吗?


1
你是指如果从横屏切换到竖屏,或者反过来吗? - Pengume
这个线程不是相同的吗?:https://dev59.com/82445IYBdhLWcg3wdqKO - Viren
在onConfigurationChanged方法中,您可以检测到发生了哪种类型的ConfigurationChanged,例如屏幕旋转。 - mikepenz
尝试这个也行https://dev59.com/82445IYBdhLWcg3wdqKO - kannappan
3个回答

24

清单:

<activity android:name=".MyActivity" android:configChanges="screenSize|orientation|screenLayout|navigation"/>

活动:

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    Log.d("tag", "config changed");
    super.onConfigurationChanged(newConfig);

    int orientation = newConfig.orientation;
    if (orientation == Configuration.ORIENTATION_PORTRAIT)
        Log.d("tag", "Portrait");
    else if (orientation == Configuration.ORIENTATION_LANDSCAPE)
        Log.d("tag", "Landscape");
    else
        Log.w("tag", "other: " + orientation);

    ....
}

也可以尝试这个链接

如何检测屏幕旋转


谢谢您的回复。但是,在发布问题之前,我已经尝试过了,这对我没有用,不知道为什么,但onConfigurationChanged并不总是被调用。它只在方向与原始方向相同时才被调用(每隔一次)。 - MerlinBG
你在哪里测试它?你能发一下你的代码吗?因为我在手机上测试过了,它可以正常工作。 - Finuka
我正在模拟器上进行测试,并查看LogCat视图。 代码与上面发布的完全相同,没有方向检查: <code>android:configChanges =“orientation | keyboard”</code> 在清单中(仅针对此活动)和 <code>@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); Log.d(" - ", "onConfigChanged"); // TODO remove } </code> 在活动类中... - MerlinBG
附加信息:以下是LogCat的输出:`Activity first init: 08-01 10:50:11.920: DEBUG/-(279): onCreate 08-01 10:50:11.940: DEBUG/-(279): onStart第一次旋转: 08-01 10:51:13.420: DEBUG/-(279): onPause 08-01 10:51:13.450: DEBUG/-(279): onCreate 08-01 10:51:13.510: DEBUG/-(279): onStart 08-01 10:51:13.510: DEBUG/-(279): onRestore第二次旋转: 08-01 10:52:37.970: DEBUG/-(279): onPause 08-01 10:52:37.980: DEBUG/-(279): onCreate 08-01 10:52:38.000: DEBUG/-(279): onStart 08-01 10:52:38.153: DEBUG/-(279): onConfigChanged` - MerlinBG
跳...这就是为什么...对我来说,在模拟器上不起作用,但在真实手机上可以。也许你应该看看是否有任何相关的错误。 - Finuka
我找到了这个链接:http://code.google.com/p/android/issues/detail?id=13189。看起来模拟器存在问题。 - Finuka

8

使用onConfigurationChanged方法检测屏幕旋转并不是一个好主意。当用户旋转屏幕时,这个活动的配置更改不会正确地工作。

因此,我使用这个解决方案来解决这个问题。

public class SampleActivity extends AppCompatActivity {
    public static final String KEY_LAST_ORIENTATION = "last_orientation";
    private int lastOrientation;

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

        if (savedInstanceState == null) {
            lastOrientation = getResources().getConfiguration().orientation;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        checkOrientationChanged();
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        lastOrientation = savedInstanceState.getInt(KEY_LAST_ORIENTATION);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(KEY_LAST_ORIENTATION, lastOrientation);
    }

    private void checkOrientationChanged() {
        int currentOrientation = getResources().getConfiguration().orientation;
        if (currentOrientation != lastOrientation) {
            onScreenOrientationChanged(currentOrientation);
            lastOrientation = currentOrientation;
        }
    }

    public void onScreenOrientationChanged(int currentOrientation) {
        // Do something here when screen orientation changed
    }
}

但这段代码还不够好,不能在我的项目中使用,因此我将其应用于我的自定义库 (https://github.com/akexorcist/ScreenOrientationHelper)。

compile 'com.akexorcist:screenorientationhelper:<latest_version>'

您可以像这样创建基础活动类:

您可以创建一个基本的活动类,如下所示

    public class BaseActivity extends AppCompatActivity implements ScreenOrientationHelper.ScreenOrientationChangeListener {
    private ScreenOrientationHelper helper = new ScreenOrientationHelper(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        helper.onCreate(savedInstanceState);
        helper.setScreenOrientationChangeListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        helper.onStart();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        helper.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        helper.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    public void onScreenOrientationChanged(int orientation) {

    }
}

然后使用这个基类扩展活动。
public class MainActivity extends BaseActivity {

    ...

    @Override
    public void onScreenOrientationChanged(int orientation) {
        // Do something when screen orientation changed
    }
}

完成!


1

你为什么不试试这个呢?

onCreated中获取手机的方向:

Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
myOrientation = display.getOrientation();

然后,重写方法onConfigurationChanged并检查方向是否已更改:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    if(newConfig.orientation != myOrientation)
        Log.v(tag, "rotated");
    super.onConfigurationChanged(newConfig);
}

不要忘记在活动中的清单文件中添加android:configChanges="orientation"。该配置可以防止屏幕旋转时活动被销毁和重建。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接