Java 检查Android电视盒

Java 检查Android电视盒,java,android,Java,Android,我试图区分Android智能手机、平板电脑和电视盒。 有没有办法检查哪个设备类型的应用程序正在运行?是的,您可以使用以下方法检测设备类型: 在维度的文件中添加布尔值以检测平板电脑: 在sw720dp/dimens.xml和sw600dp/dimens.xml中添加此行 <bool name="isTablet">true</bool> 如果isAndroidTv为false,isTablet为false,则设备可能是移动设备 <bool name="isTable

我试图区分Android智能手机、平板电脑和电视盒。
有没有办法检查哪个设备类型的应用程序正在运行?

是的,您可以使用以下方法检测设备类型:

在维度的文件中添加布尔值以检测平板电脑: 在sw720dp/dimens.xml和sw600dp/dimens.xml中添加此行

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

如果isAndroidTv为false,isTablet为false,则设备可能是移动设备
<bool name="isTablet">false</bool>
boolean isTablet;
boolean isAndroidTv;

isTablet = (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
if (!isTablet) {
    isTablet = context.getResources().getBoolean(R.bool.isTablet);
}

UiModeManager uiModeManager = (UiModeManager) (context.getSystemService(UI_MODE_SERVICE));
if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
    isAndroidTv = true;

} else if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEVISION) || context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK)) {
    isAndroidTv = true;
} else if (!context.getPackageManager().hasSystemFeature("android.hardware.touchscreen")) {
    isAndroidTv = true;
} else if (!context.getPackageManager().hasSystemFeature("android.hardware.telephony")) {
    isAndroidTv = true;
}