Nhờ @Codeversed, mà xuất sắc trả lời là rất gần với làm việc (nhưng kHÔNG như hình), tôi có một MainActivity
rằng hoạt động tốt. Tất cả những gì cần làm là di chuyển .enable
đến nơi nó có thể được thực hiện bên ngoài khối on
, chẳng hạn như ngay sau khi khai báo myOrientationEventListener
.
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
{
public static boolean PORTRAIT_MODE = true;
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)
{
PORTRAIT_MODE = ((orientation < 100) || (orientation > 280));
Log.w("Orient", orientation + " PORTRAIT_MODE = " + PORTRAIT_MODE);
}
};
Log.w("Listener", " can detect orientation: " + myOrientationEventListener.canDetectOrientation() + " ");
myOrientationEventListener.enable();
}
}
Bạn muốn tìm thời gian chạy ứng dụng để kiểm tra thiết bị thời tiết là ảnh ngang hay dọc? – Herry
có thể trùng lặp của [Làm thế nào để kiểm tra thiết bị tự nhiên (mặc định) định hướng trên Android (tức là có được phong cảnh cho ví dụ, Motorola Charm hoặc Flipout)] (http://stackoverflow.com/questions/4553650/how-to-check-device- tự nhiên mặc định-định hướng-trên-android-ie-get-landscape) – weston