根据高度和宽度设置设备的方向

Setting the Orientation of the device based on Height and width

在我的应用程序中,我遇到的情况是所有 phone 都只有肖像,平板电脑的高度 = 1024; width = 768 然后 Height/width(1024/768<=1.3333) 我想制作横向选项卡,否则我需要制作纵向。

String userAgent = new WebView(activity).getSettings()
            .getUserAgentString();
double screen_size = 1.3333333333333333;
if (userAgent.contains("Mobile")) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mEditor.putString("Device_Mode", "Phone");
        mEditor.commit();
    } else {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        Display display = activity.getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getRealSize(size);
        double height = size.y;
        double width = size.x;
        double aspect_Ratio = width / height;
        if (aspect_Ratio <= screen_size) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
            mEditor.putString("Device_Mode", "TAB-LANDSCAPE");
            mEditor.commit();
            quit();
        } else {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            mEditor.putString("Device_Mode", "TAB-PORTRAIT");
            mEditor.commit();
            quit();
        }
}

屏幕越来越闪烁,应用程序终于崩溃了。

使用以下代码检查手机或平板电脑的天气。

TelephonyManager manager =(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if(manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE){
return "Tablet";
}else{
return "Mobile";
}

屏幕一直在切换方向,一直在闪烁。所以我路过

android:configChanges="orientation|keyboardHidden|screenSize" 

将此行代码放入我的清单中相应的 activity。所以 activity 闪烁停止了。

int i = getResources().getConfiguration().orientation;
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getRealSize(size);
double height = size.y;
double width = size.x;
double aspect_Ratio = 0.0;
if (i == 1)
    aspect_Ratio = height / width;
else if (i == 2)
    aspect_Ratio = width / height;
if (aspect_Ratio <= screen_size) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

通过使用这段代码,我完成了我的任务:)