开发者

Getting the actual orientation of the phone

开发者 https://www.devze.com 2023-04-10 14:22 出处:网络
I need to determine the actual orientation of the phone which is running my app and I can\'t find any way to do it. I have found the \"getRequestedOrientation\" function but it only retrieves the orie

I need to determine the actual orientation of the phone which is running my app and I can't find any way to do it. I have found the "getRequestedOrientation" function but it only retrieves the orientation set by the "setRequestedOrientation" function.

In my app, I let the phone choose the orientation according to it's physical orientation. And I have some customizing to do depending o开发者_开发知识库n the result.

How can I do?

Thanks in advance for the time you will spend trying to help me.


According to these two questions:

  • Check orientation on Android phone
  • How can I get the current screen orientation?

you can use:

Activity.getResources().getConfiguration().orientation

According to the comment on the second question, however, this value returns only landscape or portrait. If you need reverse orientation as well, I would suggest querying the accelerometer values to detect the difference. However, I'm not familiar with Android and accelerometers enough to suggest how to do so exactly.


Use getRotation()


Its really to get the current orientation.

Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
int orientation = display.getOrientation();


See this, this, and this stackoverflow question. They all address how to get the Android phone orientation and the second one includes a detailed explanation of how the Android compass and orientation system works.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号