检测Android设备是否使用导航控件

时间:2014-03-31 16:11:27

标签: android

这是我一直在努力的事情。许多中国Android制造商都使用带键盘控制的遥控器来创建Android电视盒。

我正在寻找一种确定方法来检测设备是否使用导航控件,或者实际上是否正在使用触摸屏输入。某些设备也可能将触摸屏输入模拟为鼠标输入,所以它有点棘手。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

请阅读以下文章中的输入控件:

要在运行时查看用户可用的导航类型,请使用Configuration类。

Configuration configuration = context.getResources().getConfiguration();
if (Configuration.NAVIGATION_NONAV == configuration.navigation) {
    // Device has no navigation facility other than using the touchscreen.
} else if (Configuration.NAVIGATION_DPAD == configuration.navigation) {
    // Device has a directional-pad (d-pad) for navigation.
} else if (Configuration.NAVIGATION_TRACKBALL == configuration.navigation) {
    // Device has a trackball for navigation.
} // ... etc

答案 1 :(得分:1)

根据Jozua的回答,我创建了这个简单的方法,可用于确定设备是否使用了多种因素的导航控件。代码是以尽早尝试失败的方式编写的。

/**
 * Determines if the device uses navigation controls as the primary navigation from a number of factors.
 * @param context Application Context
 * @return True if the device uses navigation controls, false otherwise.
 */
public static boolean usesNavigationControl(Context context) {
    Configuration configuration = context.getResources().getConfiguration();
    if (configuration.navigation == Configuration.NAVIGATION_NONAV) {
        return false;
    } else if (configuration.touchscreen == Configuration.TOUCHSCREEN_FINGER) {
        return false;
    } else if (configuration.navigation == Configuration.NAVIGATION_DPAD) {
        return true;
    } else if (configuration.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH) {
        return true;
    } else if (configuration.touchscreen == Configuration.TOUCHSCREEN_UNDEFINED) {
        return true;
    } else if (configuration.navigationHidden == Configuration.NAVIGATIONHIDDEN_YES) {
        return true;
    } else if (configuration.uiMode == Configuration.UI_MODE_TYPE_TELEVISION) {
        return true;
    }
    return false;
}

我已经在众多手机,平板电脑,仿真器配置和Google TV上对此进行了测试。使用遥控器和USB鼠标控制许多设备。我还没有测试它是否在这些设备上按预期工作。

相关问题