昨晚,当我旋转Android设备时,此代码(which I found here)显示了各种Log
输出,值从0到360,可能还有一些底片。
但它今天不起作用。没有Log
输出。我在Log
声明中加了一个断点;调试时没有停止。
我今天早上更改了几行,根据orientation
的值设置了一个值,然后问题(没有输出)浮出水面,所以我使用Show history
恢复为{{昨晚最后一次活跃的1}}但是在调试时没有输出并且在MainActivity.java
语句处没有停止。
是什么解释了这个?
Log
**编辑**
根据要求,这里是import android.app.Activity;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.OrientationEventListener;
public class MainActivity extends Activity
{
OrientationEventListener myOrientationEventListener ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myOrientationEventListener = new OrientationEventListener(this,
SensorManager.SENSOR_DELAY_NORMAL)
{
@Override
public void onOrientationChanged(int orientation)
{
Log.w("Orient", orientation + "");
myOrientationEventListener.enable();
}
};
}
}
,根据AndroidManifest.xml
,自2月19日以来没有变化,但我很乐意添加许可;但昨晚它是如何运作的? [另外:按照第一个答案中的建议,在Show history
内添加了突出显示的行。]
<activity
这是Android Studio 1.5.1输出:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dslomer64.nowweregettinsomewheres.fragments"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.dslomer64.nowweregettinsomewheres.fragments.MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
你的清单中需要configChanges =“orientation”。
答案 1 :(得分:0)
显然,Show history
不一定包含对代码的最后更改。显然,在昨晚赶到预约之前,我已将.enable()
声明移到了on
区块之外。代码现在完美无缺,就像它昨晚没有更改AndroidManifest.xml
一样。
myOrientationEventListener = new OrientationEventListener(this,
SensorManager.SENSOR_DELAY_NORMAL)
{
@Override
public void onOrientationChanged(int orientation)
{
///////// NOT HERE myOrientationEventListener.enable(); //////////
Log.w("Orient", orientation + "");
}
};
Log.w("Listener", " can detect orientation: " + myOrientationEventListener.canDetectOrientation() + " " );
myOrientationEventListener.enable(); // HERE ////////////////////////
我要求编辑代码来自的答案,以免下一个使用它的人。只要enable
被逻辑放置,答案就可以了。
* HOWEVER(编辑)*
在我的onCreate
方法中,该听众无论如何都不能正常工作。正如@Gavriel所说,我找到了一个更好的方法:
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) ... do whatever