2010-09-09 16 views
31

Có cách nào để tìm hiểu xem thiết bị có phải là dọc hoặc ngang theo mặc định không? Trong đó tôi có nghĩa là cách bạn thường sử dụng thiết bị.Tìm hiểu xem thiết bị Android có ở chế độ dọc hoặc ngang để sử dụng bình thường không?

Hầu hết các điện thoại đều có màn hình dọc để sử dụng bình thường nhưng có một số cờ để tìm ra điều đó không?

+0

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

+0

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

Trả lời

2
package com.android.portraitandlandscape; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Display; 
import android.widget.Toast; 

public class PortraitLandScape extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth(); 
    int height = display.getHeight(); 

    Log.v("log_tag", "display width is "+ width); 
    Log.v("log_tag", "display height is "+ height); 

    if(width<height){ 
     Toast.makeText(getApplicationContext(),"Device is in portrait mode",Toast.LENGTH_LONG).show(); 
    } 
    else{ 
     Toast.makeText(getApplicationContext(),"Device is in landscape mode",Toast.LENGTH_LONG).show(); 
    } 

} 
} 

Bạn có thể thử mã này để kiểm tra xem thiết bị ở chế độ ngang hay dọc.

116

Bạn có thể làm điều này bằng cách:

Đối Lanscape

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){ 
    //Do some stuff 
} 

Đối Portrait

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){ 
    //Do some stuff 
} 

Kiểm tra: http://developer.android.com/reference/android/content/res/Configuration.html#orientation

+2

Điều này không hiệu quả đối với tôi khi một trong các hoạt động của tôi bị khóa theo một hướng cụ thể trong Tệp kê khai. Ví dụ: nếu nó bị khóa thành "Chân dung", nó sẽ trả về Configuration.ORIENTATION_PORTRAIT mặc dù thiết bị của tôi là máy tính bảng ngang. – jrequejo

+1

Làm việc như một nhà vô địch vào năm 2017. –

+0

@jrequejo Có thể sử dụng dữ liệu con quay hồi chuyển? – zundi

0

Tôi không chắc chắn nếu điều này sẽ giúp đỡ nhưng đây là một ví dụ nhanh tôi đã viết sẽ cho bạn thấy làm thế nào để có một người nghe ... mà sẽ cho phép bạn tìm ra định hướng bạn đang ở.

...textviewOrientation = (TextView)findViewById(R.id.textView1); 

    myOrientationEventListener = new OrientationEventListener(this, 
       SensorManager.SENSOR_DELAY_NORMAL){ 

     @Override 
     public void onOrientationChanged(int arg0) { 

     textviewOrientation.setText("Orientation: " + String.valueOf(arg0)); 
     myOrientationEventListener.enable(); 


      if ((arg0 < 100) || (arg0 > 280)){ 

       setRequestedOrientation(
        ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

      } else { 

       setRequestedOrientation(
        ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

      } 


     } 

    };... 


[email protected] 
protected void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 
    myOrientationEventListener.disable(); 
}... 
2

Trong hành động của bạn, hãy làm như sau:

if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) 

ví dụ ...

1

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(); 
    } 
} 
1

Tôi gặp sự cố tương tự. Giải quyết nó bằng cách so sánh các giá trị của quay và định hướng. Không cần sử dụng cảm biến hoặc bất kỳ người nghe nào, chỉ cần gọi phương thức isDefaultLandscape bất cứ khi nào bạn muốn.

private boolean isDefaultLandscape(final Context context) 
{ 
    Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
    int rotation = display.getRotation(); 
    int orientation = context.getResources().getConfiguration().orientation; 

    switch (rotation) 
    { 
     case Surface.ROTATION_180: 
     case Surface.ROTATION_0: 
     { 
      return orientation == Configuration.ORIENTATION_LANDSCAPE; 
     } 
     default: 
     { 
      return orientation == Configuration.ORIENTATION_PORTRAIT; 
     } 
    } 
}