【问题标题】:How to find Tablet or Phone in Android , Programmatically?如何以编程方式在 Android 中查找平板电脑或手机?
【发布时间】:2013-05-28 04:53:58
【问题描述】:

我的情况是手机和平板电脑的逻辑是相同的。但布局略有不同。我尝试使用以下代码

public static boolean findoutDeviceType(Context context) 
    {
        return (context.getResources().getConfiguration().screenLayout & 
                       Configuration.SCREENLAYOUT_SIZE_MASK)>= 
                           Configuration.SCREENLAYOUT_SIZE_LARGE;
    }

Samsung Tab 10" 的分辨率为 1280 * 800,S3 的分辨率为 1270 * 720。此代码将 Tab 和 Phone 的 Size 返回为 XLarge,因为其检查标准为 > 960 * 720。

我已经测试在 Res 的布局文件夹中插入相应的 UI 作为 Layout、Layout-Large 和 Layout-xLarge。但这无论如何都没有影响。在检查时,它从 Layout 文件夹中获取了 UI。

无论如何,即使我将 UI 放在不同的布局文件夹中,我也必须在类文件中检查它们以设置相应的 ContentView。

还有其他方法可以找到吗?

【问题讨论】:

标签: android android-layout


【解决方案1】:

Android 培训中讨论了这个主题:

http://developer.android.com/training/multiscreen/screensizes.html#TaskUseSWQuali

Here is implementation,

感谢ol_v_er 这种简单易行的方法。

一些附加信息

您现在有标志表明您的应用程序是在手机还是平板电脑上运行。

我创建了两个包来处理 UI 及其功能,

com.phone
com.tablet

然后你将控制重定向到你需要的包

boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (tabletSize) {
    // do something
    //Start activity for tablet
} else {
    // do something else
    //Start activity for phone     
}

Refer

注意:我认为对于 10 英寸和 7 英寸屏幕的应用程序都从 res/values-sw600dp/ 获取资源。但更具体地说,我认为对于 10 英寸平板电脑屏幕我们可以使用res/values-sw720dp/

<resources>
    <bool name="isTablet">true</bool>
</resources>

【讨论】:

【解决方案2】:
public boolean isTablet() { 
try { 
    // Compute screen size 
 Context context = this;
    DisplayMetrics dm = 
 context.getResources().getDisplayMetrics(); 
    float screenWidth  = dm.widthPixels / dm.xdpi; 
    float screenHeight = dm.heightPixels / dm.ydpi; 
    double size = Math.sqrt(Math.pow(screenWidth, 2) + 
                            Math.pow(screenHeight, 2)); 
    // Tablet devices have a screen size greater than 6 
      inches 
    return size >= 6; 
  } catch(Throwable t) { 
    Log.e("Failed to compute screen size", t.toString()); 
    return false; 
  } 

}

【讨论】:

    【解决方案3】:

    试试这个

        public boolean isTablet(Context context) {
            boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
            boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
            return (xlarge || large);
        }
    

    如果您使用的是平板电脑,它将返回 true。已在三星 Galaxy Tab 7" 和三星 Galaxy S3 上进行过检查。

    【讨论】:

    • 检查两台设备不是足够好的检查。小心这种方法,因为它不可靠。
    • 基于设备尺寸是确定设备的最糟糕的想法。我不知道为什么这是正确的答案。
    【解决方案4】:

    老问题,但这可能会对某人有所帮助。 如果您想了解设备是平板电脑(屏幕大于 7 英寸)还是手机,您可以使用此 util 方法:

    科特林

    fun isTablet(): Boolean {
        return App.instance.resources.configuration.smallestScreenWidthDp >= 600
    }
    

    Java

    public static Boolean isTablet(){
        return App.instance.resources.configuration.smallestScreenWidthDp >= 600
    }
    

    App.instance 是应用实例。

    【讨论】:

    • 这些值不代表设备的物理尺寸。在多窗口模式下,它们会变小。
    • Java 代码引用错误。首先,它不是 Java 代码。致发帖者:如果你想分享任何不是你写的代码,请彻底理解,然后再冲出去。我也同意“Miloš Černilovský”的评论:这些值不代表物理大小。多窗口模式下的解决方案肯定是错误的
    【解决方案5】:

    例如,您可以设置一些 res-values 文件夹:

    res/values-xlarge res/values-大 res/values-sw600dp

    等等。然后你可以为每个声明一个布尔值:

        <resources>
    <bool name="isXLarge">true</bool>
        </resources>
    

        <resources>
    <bool name="isLarge">true</bool>
        </resources>
    

    你可以通过

       boolean xlargeValue = getResources().getBoolean(R.bool.isXlarge);
       boolean largevalue = getResources().getBoolean(R.bool.isLarge);
       boolean tabletValue = getResources().getBoolean(R.bool.sw620dp):
    

    【讨论】:

      【解决方案6】:

      使用不同的资源文件而不是尝试以编程方式确定它。这对于大多数情况来说已经足够了,这也是documentation 的建议。

      查看我的完整答案here

      【讨论】:

        【解决方案7】:

        这在我的应用中运行良好:

        private boolean isPhoneDevice(){
        
            return getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
        
        }
        

        【讨论】:

        • 这仅检查设备是否可以用作电话。定义:“该设备具有支持数据通信的电话收音机。”。一些屏幕非常大的平板电脑和手机将在此返回 true。
        【解决方案8】:

        试试这个代码,你的应用正在运行的设备手机或平板电脑很容易在里面调用 oncreate() 方法

        isTabletDevice(活动)

        private static boolean isTabletDevice(Context activityContext) {
        
            boolean device_large = ((activityContext.getResources().getConfiguration().screenLayout &
                  Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE)
            DisplayMetrics metrics = new DisplayMetrics();
            Activity activity = (Activity) activityContext;
            activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
            if (device_large) {
                //Tablet
                if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT){              
                    return true;
                }else if(metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM){
                    return true;    
                }else if(metrics.densityDpi == DisplayMetrics.DENSITY_TV){
                    return true;    
                }else if(metrics.densityDpi == DisplayMetrics.DENSITY_HIGH){
                    return true;    
                }else if(metrics.densityDpi == DisplayMetrics.DENSITY_280){
                    return true;    
                }else if(metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {
                    return true;
                }else if(metrics.densityDpi == DisplayMetrics.DENSITY_400) {
                    return true;
                }else if(metrics.densityDpi == DisplayMetrics.DENSITY_XXHIGH) {
                    return true;
                }else if(metrics.densityDpi == DisplayMetrics.DENSITY_560) {
                    return true;
                }else if(metrics.densityDpi == DisplayMetrics.DENSITY_XXXHIGH) {
                    return true;
                }            
            }else{
                //Mobile
                 } 
            return false;
        }
        

        【讨论】:

          【解决方案9】:

          试试这个代码。您可以获取屏幕英寸,根据尺寸您可以获取平板电脑或安卓设备

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

          【讨论】:

          • 是的,但如果只有这两种设备(比如三星 S3 和三星 10" 平板电脑)我们可以比较,但我如何为其他设备做也......有各种各样各种设备的分辨率大小。S4设备屏幕更大。
          • 但是android设备尺寸不像平板电脑尺寸,所以你只能得到这个是android移动设备或平板电脑。
          【解决方案10】:

          所有其他问题都使用资源限定符和方法,它们不代表设备的物理尺寸,而是可用的屏幕尺寸。例如,在多窗口模式下,系统将从“values”文件夹而不是“values-large”获取资源,因为应用程序的可用屏幕尺寸变小了。要确定物理设备是平板电脑还是手机,请使用以下方法(我使用640x480dp作为平板电脑的最小尺寸,这是large设备的定义,随意更改这些常量):

          fun isTablet(context: Context): Boolean {
              val outSize = Point()
              val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
              windowManager.defaultDisplay.getRealSize(outSize)
              outSize.x = pxToDp(windowManager, outSize.x)
              outSize.y = pxToDp(windowManager, outSize.y)
              val shorterSideDp: Int
              val longerSideDp: Int
              if (outSize.x > outSize.y) {
                  shorterSideDp = outSize.y
                  longerSideDp = outSize.x
              } else {
                  shorterSideDp = outSize.x
                  longerSideDp = outSize.y
              }
              return shorterSideDp > 480 && longerSideDp > 640
          }
          

          PX转DP函数:

          @Dimension(unit = Dimension.DP)
          fun pxToDp(windowManager: WindowManager, @Dimension(unit = Dimension.PX) px: Int): Int {
              val displayMetrics = DisplayMetrics()
              windowManager.defaultDisplay.getRealMetrics(displayMetrics)
              return (px / displayMetrics.densityDpi.toFloat() * DisplayMetrics.DENSITY_DEFAULT).roundToInt()
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-11-09
            • 1970-01-01
            • 2012-07-06
            • 2012-12-08
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多