【问题标题】:How to know whether its a tablet or phone in android programmatically? [duplicate]如何以编程方式知道它是android中的平板电脑还是手机? [复制]
【发布时间】:2012-03-27 06:41:28
【问题描述】:

我想在 android 中检测给定的设备是平板电脑还是手机。我在模拟器中尝试了这两种,但都没有奏效。两者都在这里:

第一

if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) 
{
    //code
}

第二

    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;                   
}

【问题讨论】:

  • 我上面已经提到了上面我试过的两种方法,它没有用...我想从android程序中获取它,只是不使用用户代理...

标签: android device tablet screen-size android-screen


【解决方案1】:

试试这个代码。你可以得到屏幕英寸

    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+"");

【讨论】:

    【解决方案2】:

    res 文件夹中创建两个布局文件夹,例如 layoutlayout_xlarge。在 layout-xlarge 中创建一个不需要的视图strong> 文件。以编程方式获取此不需要的视图 id。如果您得到 空指针异常,则在 try catch 块中的代码中访问该不需要的 id,那么它是 小型设备 否则它是 平板电脑。还有一件事是在 layout-xlarge 屏幕中隐藏不需要的视图。

    【讨论】:

      【解决方案3】:

      我认为要回答的第一个问题是您所说的“手机”或“平板电脑”是什么意思。我们的想法可能有一些区别,但实际上,您的应用程序应该不在乎。 Asus Transformer 是平板电脑,Samsung Galaxy Nexus 是手机。 Samsung Galaxy Note 是什么? Engadget 和其他人称其为“phablet”——在这种情况下,您的应用程序应该假设什么?

      您对“平板电脑”的定义是不是任何没有 SIM 卡的设备?这也是一个错误的定义,因为有许多平板电脑确实具有内置 SIM 卡。您对“平板电脑”的定义是否是默认为横向模式并旋转为纵向模式的任何设备?也许这是一种方法,但我怀疑这是否足够,因为将来可能会有默认为纵向的平板电脑,或者更糟的是,将来会有方形外形。

      在我看来,处理此问题的最佳方法是使用 Android's guidelines 并打开 UI 布局。如果您认为您对平板电脑与手机有明确的定义,并且绝对需要检测其中一个,您可能需要执行一些操作,例如提取型号名称from the browser's user agent string,然后将其与您维护的平板电脑名称数据库进行匹配.我知道。呸。

      【讨论】:

        【解决方案4】:

        将此方法放在onResume()中即可查看。

        public double tabletSize() {
        
             double size = 0;
                try {
        
                    // Compute screen size
        
                    DisplayMetrics dm = context.getResources().getDisplayMetrics();
        
                    float screenWidth  = dm.widthPixels / dm.xdpi;
        
                    float screenHeight = dm.heightPixels / dm.ydpi;
        
                    size = Math.sqrt(Math.pow(screenWidth, 2) +
        
                                         Math.pow(screenHeight, 2));
        
                } catch(Throwable t) {
        
                }
        
                return size;
        
            }
        

        通常平板电脑在 6 英寸大小后开始。

        【讨论】:

          猜你喜欢
          • 2022-11-30
          • 2016-11-09
          • 2013-05-28
          • 2012-07-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-31
          • 2023-03-20
          相关资源
          最近更新 更多