【发布时间】:2018-10-21 19:23:13
【问题描述】:
如何以编程方式区分 AndroidTV 上的电视和机顶盒/游戏机?此方法 (https://developer.android.com/training/tv/start/hardware) 不起作用,因为运行 AndroidTV 的 STB 被视为电视。
【问题讨论】:
标签: android android-tv television
如何以编程方式区分 AndroidTV 上的电视和机顶盒/游戏机?此方法 (https://developer.android.com/training/tv/start/hardware) 不起作用,因为运行 AndroidTV 的 STB 被视为电视。
【问题讨论】:
标签: android android-tv television
这样做的明显原因是确定设备是否适合玩游戏。如果是出于任何其他原因,则需要详细说明目的以便获得适用的帮助。
由于无法直接查询设备类型 - 就我个人而言,我会寻找只有游戏机才有的东西。
换句话说:游戏控制器/游戏手柄。
public ArrayList<Integer> getGameControllerIds() {
ArrayList<Integer> gameControllerDeviceIds = new ArrayList<Integer>();
int[] deviceIds = InputDevice.getDeviceIds();
for (int deviceId : deviceIds) {
InputDevice dev = InputDevice.getDevice(deviceId);
int sources = dev.getSources();
// Verify that the device has gamepad buttons, control sticks, or both.
if (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
|| ((sources & InputDevice.SOURCE_JOYSTICK)
== InputDevice.SOURCE_JOYSTICK)) {
// This device is a game controller. Store its device ID.
if (!gameControllerDeviceIds.contains(deviceId)) {
gameControllerDeviceIds.add(deviceId);
}
}
}
return gameControllerDeviceIds;
}
当然,这不是万无一失的。显然,如果当时拔掉游戏手柄(不确定何时会发生),则不会返回任何内容。更不用说,一些电视支持游戏手柄(首先想到的是三星)——但是,如果您打算验证应用程序是否有足够的输入可用,那么这将是理想的。
如果游戏手柄不存在,可能会显示一条消息,说明“请连接游戏手柄”。 -- 在后台不断检查,一旦检测到就自动继续。
【讨论】: