我正在尝试编写.apk来衡量旋转Android设备屏幕所需的时间(循环几次)。
我正在调用setRequestedOrientation()
来重新定位4个位置的屏幕(PORTRAIT
,LANDSCAPE
,REVERSE_PORTRAIT
和REVERSE_LANDSCAPE
)。
我的问题是我无法分辨轮换结束的时间。我尝试了一些解决方案,但每个解决方案都存在问题。
onConfigurationChanged()
。问题是当方向变化发生在传感器上而不是setRequestedOrientation()
时,会触发此方法。实际上,我必须使用setRequestedOrientation()
FULL_SENSOR
参数调用onConfigurationChanged
,以便在我之前调用setRequestedOrientation()
之后工作。setRequestedOrientation()
种“阻塞”。我在android.view.Display.getRotation()
之前调用了setRequestedOrientation()
并使用while循环阻塞,直到它发生了变化。不幸的是,它也不起作用。它在重新显示UI之前返回。onCreate()
来跟踪活动的生命周期,以假设轮换已完成。以前,我正在循环,直到我的计数器达到一个值,在每次迭代中调用setRequestedOrientation()
。在这种情况下,我的while循环变为if语句,我假设onCreate
每次都会调用该函数,但是onCreate()
在几次迭代后不会被调用。用户界面似乎也没有重新绘制。waitForIdleSync()
之后立即使用setRequestedOrientation()
方法。 waitForIdleSync()
是Instrumentation类的一部分,我认为这只是用于测试而不是用于标准.apk。任何想法都会非常受欢迎。我非常感谢你的帮助。
答案 0 :(得分:0)
你应该尝试使用: http://developer.android.com/reference/android/view/OrientationEventListener.html
您首先需要调用enable(),然后您会收到设备每次轮换更改的通知。
答案 1 :(得分:0)
没有直接的方式来了解setRequestedOrientation()
何时通过Android API完成。要知道的唯一方法是在调用之前和在onCreate()
中调用它之后检查当前方向。
在Activity
班级中执行:
long timeStamp;
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
timeStamp = System.currentTimeMillis();
}
else {
timeStamp = savedInstanceState.getLong("state_time_stamp");
}
// For example, check how long it takes to switch from portrait to landscape.
// You can modify this part to check PORTRAIT, LANDSCAPE, REVERSE_PORTRAIT and REVERSE_LANDSCAPE.
final int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.SCREEN_ORIENTATION_PORTRAIT) {
setRequestedOrientation(ActivityInfo.ORIENTATION_LANDSCAPE);
return;
}
// We will reach here only after setRequestedOrientation() is done
// and here's the time it took to change the orientation:
final long elapsedTime = System.currentTimeMillis() - timeStamp;
}
public void onSaveInstanceState(Bundle outState) {
outState.putLong("state_time_stamp", timeStamp);
}