如何检测设备是Android手机还是Android平板电脑?

KAP*_*OID 55 android device-detection

我有两个适用于Android平板电脑和Android手机的应用.对于平板电脑的应用程序我设置android:minSdkVersion="11".但是现在像Galaxy S3这样的Android手机拥有Android 4.0.4版本,因此S3用户可以从Google Play商店下载我的平板电脑应用程序.我想警告手机用户在安装平板电脑应用程序时下载手机应用程序.反之亦然平板电脑用户在运行手机应用时下载平板电脑应用.

有没有简单的方法来检测设备类型?

编辑:

我找到了这个链接的解决方案.

在您的清单文件中,您可以为手机和平板电脑声明屏幕功能,然后Google Play会决定手机和平板电脑的下载权限.

Ale*_*ood 129

用这个:

public static boolean isTablet(Context context) {
    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
Run Code Online (Sandbox Code Playgroud)

如果设备在大屏幕上运行,它将返回true.

其他一些有用的方法可以在这里找到.

  • 尽管如此,我认为所提供的代码可能会错误地将大型手持设备分类为平板电脑......显然,手机和平板电脑之间的差异变得非常模糊,所以现在应该不那么重要了 (8认同)
  • 请勿使用此方法,因为它无法将Samsung Galaxy Mega设备检测为手机. (6认同)
  • 使用约代码Nexus 7不被视为平板电脑..! (4认同)

Bha*_*iya 20

您也可以在资源文件中尝试此
添加布尔参数.
在res/values/dimen.xml文件中添加此行

<bool name="isTab">false</bool>
Run Code Online (Sandbox Code Playgroud)

而在res/values-sw600dp/dimen.xml文件中添加以下行:

<bool name="isTab">true</bool>
Run Code Online (Sandbox Code Playgroud)

然后在你的java文件中获取此值:

if(getResources().getBoolean(R.bool.isTab)) {
    System.out.println("tablet");
} else {
    System.out.println("mobile");
}
Run Code Online (Sandbox Code Playgroud)

  • 这应该是公认的答案,请参阅:http://stackoverflow.com/questions/9279111/determine-if-the-device-is-a-smartphone-or-tablet (3认同)

Paw*_*ari 7

此代码段将告知设备类型是7英寸或更高以及Mdpi或更高分辨率.您可以根据需要更改实施.

 private static boolean isTabletDevice(Context activityContext) {
        boolean device_large = ((activityContext.getResources().getConfiguration().screenLayout &
                Configuration.SCREENLAYOUT_SIZE_MASK) ==
                Configuration.SCREENLAYOUT_SIZE_LARGE);

        if (device_large) {
            DisplayMetrics metrics = new DisplayMetrics();
            Activity activity = (Activity) activityContext;
            activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

            if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT
                    || metrics.densityDpi == DisplayMetrics.DENSITY_HIGH
                    || metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM
                    || metrics.densityDpi == DisplayMetrics.DENSITY_TV
                    || metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {
                AppInstance.getLogger().logD("DeviceHelper","IsTabletDevice-True");
                return true;
            }
        }
        AppInstance.getLogger().logD("DeviceHelper","IsTabletDevice-False");
        return false;
    }
Run Code Online (Sandbox Code Playgroud)

  • 您的代码不会将具有[SCREENLAYOUT_SIZE_XLARGE](http://developer.android.com/reference/android/content/res/Configuration.html#SCREENLAYOUT_SIZE_XLARGE)的设备检测为平板电脑(例如Nexus 10).您应该将比较更改为`Configuration.SCREENLAYOUT_SIZE_MASK>> = Configuration.SCREENLAYOUT_SIZE_LARGE)`以解决此问题. (2认同)

归档时间:

查看次数:

82541 次

最近记录:

7 年,1 月 前