如何以编程方式知道它是平板电脑还是手机?

Kar*_*hik 3 android device tablet screen-size android-screen

我想要检测给定的设备是否是Android中的平板电脑或手机.我已经在模拟器中尝试了这两个但没有工作.两者都在这里:

第一

if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) 
{
    //code
}
Run Code Online (Sandbox Code Playgroud)

第二

    private boolean isTabletDevice() 
{         
    if (android.os.Build.VERSION.SDK_INT >= 11) 
    { 
    // honeycomb                    
    // test screen size, use reflection because isLayoutSizeAtLeast is only available since 11     
    Configuration con = getResources().getConfiguration();              
    try {                     
            Method mIsLayoutSizeAtLeast = con.getClass().getMethod("isLayoutSizeAtLeast");    
            Boolean r = (Boolean) mIsLayoutSizeAtLeast.invoke(con, 0x00000004); // Configuration.SCREENLAYOUT_SIZE_XLARGE    
            return r;              
        } catch (Exception x)
        {    
            return false;          
        }             
    }            
    return false;                   
}
Run Code Online (Sandbox Code Playgroud)

Zac*_*uel 7

试试这个代码.您可以获得屏幕英寸

    String inputSystem;
    inputSystem = android.os.Build.ID;
    Log.d("hai",inputSystem);
    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth();  // deprecated
    int height = display.getHeight();  // deprecated
    Log.d("hai",width+"");
    Log.d("hai",height+"");
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    double x = Math.pow(width/dm.xdpi,2);
    double y = Math.pow(height/dm.ydpi,2);
    double screenInches = Math.sqrt(x+y);
    Log.d("hai","Screen inches : " + screenInches+"");
Run Code Online (Sandbox Code Playgroud)


Ven*_*hna 6

在res文件夹中创建两个布局文件夹,如此布局和layout_xlarge.在layout-xlarge文件中创建一个不需要的视图.以编程方式获取此不需要的视图ID.如果获得空指针异常,则访问try catch blocak 中的不需要的id 然后它很小设备,否则它是平板电脑.更多的东西隐藏布局-xlarge屏幕中不需要的视图.