猿问

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

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

有什么简单的方法可以检测设备类型?


大话西游666
浏览 947回答 3
3回答

Qyouu

用这个:public static boolean isTablet(Context context) {    return (context.getResources().getConfiguration().screenLayout            & Configuration.SCREENLAYOUT_SIZE_MASK)            >= Configuration.SCREENLAYOUT_SIZE_LARGE;}如果设备在大屏幕上运行,则返回true。

倚天杖

您也可以尝试在资源文件中使用此Add boolean参数。在res / values / dimen.xml文件中,添加以下行<bool name="isTab">false</bool>在res / values-sw600dp / dimen.xml文件中,添加以下行:<bool name="isTab">true</bool>然后在您的java文件中获取以下值:if(getResources().getBoolean(R.bool.isTab)) {&nbsp; &nbsp; System.out.println("tablet");} else {&nbsp; &nbsp; System.out.println("mobile");}

梵蒂冈之花

此代码段将告诉您设备类型是否为7英寸或更大以及Mdpi或更高分辨率。您可以根据需要更改实现。&nbsp;private static boolean isTabletDevice(Context activityContext) {&nbsp; &nbsp; &nbsp; &nbsp; boolean device_large = ((activityContext.getResources().getConfiguration().screenLayout &&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Configuration.SCREENLAYOUT_SIZE_MASK) ==&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Configuration.SCREENLAYOUT_SIZE_LARGE);&nbsp; &nbsp; &nbsp; &nbsp; if (device_large) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DisplayMetrics metrics = new DisplayMetrics();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Activity activity = (Activity) activityContext;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || metrics.densityDpi == DisplayMetrics.DENSITY_HIGH&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || metrics.densityDpi == DisplayMetrics.DENSITY_TV&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AppInstance.getLogger().logD("DeviceHelper","IsTabletDevice-True");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; AppInstance.getLogger().logD("DeviceHelper","IsTabletDevice-False");&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Android
我要回答