检测设备方向时,活动的方向已被锁定 [英] Detect device orientation when Activity orientation is locked
问题描述
在我的项目,我一定要锁定一个活动的方向。 (我不能重新创建活动)
In my project, I have to lock the orientation of an activity. (I cannot recreate the activity)
我想,当用户改变说该设备的取向来显示消息:
的观点被锁定在这个位置......
I want to display a message when the user change the orientation of the device that say : "The View is locked in this position ..."
我可以很容易地与此code锁定方向:
I can easily lock the orientation with this code:
if(version >= Build.VERSION_CODES.JELLY_BEAN_MR2){
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
}else{
int orientation = activity.getResources().getConfiguration().orientation;
if(orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}else{
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
有了这个code中的视图被锁定,但是当用户更改设备的方向我无法察觉。
With this code the view is locked but I cannot detect when the user change the device orientation.
如果我用这个下code:
And if I use this following code:
@Override
public void onConfigurationChanged(Configuration newConfig) {
//Check if the orientation has change
if(newConfig.orientation != mOrientation){
Toast.makeText(this, "Message for user", Toast.LENGTH_SHORT).show();
}
//Try to lock the view but not working
newConfig.orientation = mOrientation;
//Obligatory line that reload the view and cannot be delete
//(is the biggest prolem of this code)
super.onConfigurationChanged(newConfig);
}
如果有人知道的方式来锁定观点和检测设备方向的变化,这将真正帮助我。
If someone know a way to lock the view and detect the change of the device orientation it will really help me.
感谢所有
推荐答案
你尝试过在这个问题的答案:stackoverflow.com/questions/11555101/locking-screenorientation?
have you tried the answer in this question: stackoverflow.com/questions/11555101/locking-screenorientation ?
的SensorManager
和 SensorListener
,所建议的这个问题的答案,听起来好像要走的路。
using SensorManager
and SensorListener
, as suggested in that answer, sounds to me like the way to go.
这篇关于检测设备方向时,活动的方向已被锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!