简体   繁体   English

Android方向传感器-完全关闭方向传感器

[英]Android orientation sensor - switching off the orientation sensor completely

I am wondering how to switch off the orientation sensor completely in my android app for certain activities. 我想知道如何在某些应用程序中完全关闭方向传感器。

Here is what I struggle with. 这就是我所努力的。 Let's say I set the orientation in the onCreate (or onStart/onResume) method of an activity dynamically with the following command. 假设我使用以下命令在活动的onCreate(或onStart / onResume)方法中动态设置方向。

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

What I experience is that screen is appearing on the display noticeable faster if I hold the device in a manner that would result in landscape mode according to the orientation sensor. 我的经验是,如果按方向传感器的方式握住设备会导致出现横向模式,则屏幕出现在显示屏上的速度会更快。

So what it feels like is as if the activity first wants to set the layout in the orientation as the sensor would suggest and thereafter it switches according to the setRequestedOrientation method. 因此,感觉就像活动首先要按照传感器建议的那样将布局设置为方向,然后根据setRequestedOrientation方法进行切换。 I even sometimes see this on the screen as described in a related post android - unexpected brief orientation change at switch of activity . 我什至有时会在屏幕上看到这一点,如相关的android帖子中所描述的那样,在活动切换时出现了意想不到的简短方向变化

Seems like I am too late with the setRequestedOrientation call. 似乎我对setRequestedOrientation调用为时已晚。 So I am wondering how to switch off the the orientation sensor completely to avoid a setting of the orientation prior to the setRequestedOrientation call. 所以我想知道如何完全关闭方向传感器,以避免在setRequestedOrientation调用之前设置方向。 Or as alternative, how to call setRequestedOrientation in time. 或者,如何及时调用setRequestedOrientation。 As I need this feature dynamically as decided during runtime (preference settings) I cant set the orientation fixed in the manifest. 由于我需要在运行时(首选项设置)中动态确定的此功能,因此无法设置清单中固定的方向。

Thanks a lot in advance for any hint around this issue. 提前非常感谢您提供有关此问题的任何提示。

martin 马丁

Edit: I forgot to mention, the manifest contains 编辑:我忘了提及,清单包含

android:configChanges="keyboardHidden|orientation|screenSize"

The performance impact happens because android kills your activity and then starts a new instance with the new configurations. 之所以会产生性能影响,是因为android杀死了您的活动,然后使用新配置启动了一个新实例。 a good solution to this would be to handle configuration changes manually. 一个好的解决方案是手动处理配置更改。 by adding android:configChanges="orientation" to your activity element in the manifest file Overriding onConfigurationChanged method in your activity to do the layout 通过在清单文件中的活动元素中添加android:configChanges =“ orientation”覆盖活动中的onConfigurationChanged方法以进行布局

another solution would be having tow activities , one works only in landscape the other in portrait by specifying orientation attribute in manifest file, and lunch the suitable one. 另一种解决方案是进行拖曳活动,通过在清单文件中指定方向属性,一个仅在横向中工作,另一个在纵向中工作,然后进行适当的午餐。

This is how you can set orientation at runtime as follows 这是在运行时设置方向的方法,如下所示

int currentOrientation = getRequestedOrientation();

if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
else {
   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}

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

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