以编程方式在Android活动中禁用和启用方向更改 [英] Disable and enable orientation changes in an activity in Android programatically

查看:80
本文介绍了以编程方式在Android活动中禁用和启用方向更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,可以让一些后台人员工作.当后台工作正在运行时,将显示一个进度环,如果在此期间旋转了设备,则活动将重置",我想避免这种情况.

I have an App that make some background staff. When the background work is running a progress Circle is shown, if the device is rotated during this time then the Activity is "reset" and I want to avoid that.

由于这个原因,我决定在此过程中禁用定向.我已经看到了这个问题的differends线程,但至少在我看来,没有一个有效的解决方案.

For this reason I decided disable orientation during this process. I have seen differends threads for this question but none with a valid solution, at least in my case.

发布的解决方案是关于确定活动方向的,但是您必须处理以下事实:如果使用,则不会返回REVERSE方向:

The solutions posted are about fixing the activity orientation but you have to deal with the fact that the REVERSE orientations are not returned if you use:

getResources().getConfiguration().orientation

上面的函数在PORTRAIT和REVERSE_PORTRAIT情况下都返回SCREEN_ORIENTATION_PORTRAIT(至少在我的测试中如此).

The function above returns SCREEN_ORIENTATION_PORTRAIT both for PORTRAIT and REVERSE_PORTRAIT cases (at least in my tests).

因此,最后我使用了Rotation值来处理它,所以我的代码"disable the rotation"是:

So at the end I used the Rotation value to deal with that, so my code "disable the rotation" is:

int rotation = getWindowManager().getDefaultDisplay().getRotation();

    switch(rotation) {
    case Surface.ROTATION_180:
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
        break;
    case Surface.ROTATION_270:
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);         
        break;
    case  Surface.ROTATION_0:
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        break;
    case Surface.ROTATION_90:
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        break;
    }

并再次允许定向:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

这在装有Android 4.1.2的设备上完美运行,但在装有Android 4.2.1的设备上却无法正常工作.

That works perfectly in a device with Android 4.1.2 but in a Device with Android 4.2.1 it is not working as expected.

我认为在活动实时周期中管理轮换应该是一个常见问题,但是我找不到合适的解决方案.可能是我的方向不对,所以我们非常欢迎您提供任何帮助.

I guess managing rotation in the activity live cycle should be a common issue but I have not been able to find a suitable solution. May be I'm looking to the wrong direction so any help is really welcomed.

预先感谢, 伊万.

推荐答案

如果要禁用手机的方向更改,则可以在清单中使用此代码

If you want to disable orientation changes of the phone then you may use this code in the manifest

<activity android:name=".class_name"

///如果您希望屏幕为纵向

//if you want your screen in portrait

android:screenOrientation="portrait" >

///如果要在横向模式下显示屏幕

//if you want you screen in landscape mode

android:screenOrientation="landscape" >

,如果您希望手机改变方向但阻止进程重新启动,则可以在类中使用onConfigurationChanged方法:

and if you want your phone to change orientation but prevent the process from restarting then you can use the onConfigurationChanged method in your class:

@Override
public void onConfigurationChanged(Configuration newConfig) {
   // ignore orientation change
   if (newConfig.orientation != Configuration.ORIENTATION_LANDSCAPE) {
       super.onConfigurationChanged(newConfig);
   }
}

这篇关于以编程方式在Android活动中禁用和启用方向更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆