简体   繁体   English

以编程方式在Android中禁用并启用方向更改

[英]Disable and enable orientation changes in an activity in Android programatically

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. 当后台工作正在运行时,会显示圆圈,如果在此期间旋转设备,则活动将“重置”,我想避免这种情况。

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. 我已经看到了这个问题的差异线程,但没有一个有效的解决方案,至少在我的情况下。

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: 发布的解决方案是关于修复活动方向,但您必须处理以下事实:如果您使用以下情况,则不会返回REVERSE方向:

getResources().getConfiguration().orientation

The function above returns SCREEN_ORIENTATION_PORTRAIT both for PORTRAIT and REVERSE_PORTRAIT cases (at least in my tests). 对于PORTRAIT和REVERSE_PORTRAIT情况,上面的函数都返回SCREEN_ORIENTATION_PORTRAIT(至少在我的测试中)。

So at the end I used the Rotation value to deal with that, so my code "disable the rotation" is: 所以最后我用Rotation值来处理它,所以我的代码“禁用旋转”是:

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;
    }

And to allow again the orientation: 并再次允许方向:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

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. 这在使用Android 4.1.2的设备中完美运行,但在使用Android 4.2.1的设备中,它无法按预期工作。

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. 可能是我正在寻找错误的方向,所以任何帮助都是非常受欢迎的。

Thanks in advance, Ivan. 提前谢谢,伊万。

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" >

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

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

Try this.. 试试这个..

There is no wrong in your code. 你的代码没有错。 It's correct i checked it. 这是正确的我检查了它。 Use that code in onConfigurationChanged that may give the difference and another thing is use below code to disable the activity reset. onConfigurationChanged中使用该代码可能会产生差异,另一件事是在代码下方使用以禁用活动重置。

<activity
    android:name="YourActivity"
    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:windowSoftInputMode="adjustPan"/>  

You could override the on onConfiguratoinChanged method to disable orientation changes: 您可以覆盖on onConfiguratoinChanged方法以禁用方向更改:

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

Do not forget to enable the orientation changed listener in your AndroidManifest.xml file: 不要忘记在AndroidManifest.xml文件中启用orientation更改的侦听器:

<activity android:configChanges="orientation" >

Simple: one line to add after Activity Name in android manifest 简单:在android清单中的Activity Name之后添加一行

android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale

This worked for me. 这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM