Determine if the device has touch screen or not

My app runs on standard phones but it also runs on Android players which I connect via HDMI to my TV and work with a mouse to navigate. Is there a way to programatically deterrmine whether the device has touch screen support so I can distinguish between the two ways of navigation?

I tried this, but it returns true on both devices:

getPackageManager().hasSystemFeature("android.hardware.touchscreen");

PackageManager will refer to the Android player itself which probably has a touchscreen. It cannot detect if the player display is being routed to an external monitor via HDMI. When you ask the package manager if the device has a touchscreen, it is referring to the player and not the external display.

One thing you could do is to detect if the player is connected to an external display by checking if HDMI port is active and change your navigation accordingly.

See: Detect HDMI Port in android device

Side note: If the player does not have a touchscreen but the package manager is wrongly reporting that it does, then it's either a bug with package manager or the device manufacturers implementation of Android might be buggy.


Use an OnTouchListener instead of an OnClickListener

OnTouch gives you better feedback and it does everything OnClick already does. Do this for both cases (so there is no need to figure out which case is which).

Some developers even use OnTouchListeners instead of OnClickListeners even if they're only dealing with non-touch devices (because they claim it gives them better performance and a faster response).

Personally, I don't know if that last claim is true or not, but I would agree with those developers that there is really no downside (that I'm aware of) to using an OnTouchListener instead of an OnClickListener.

链接地址: http://www.djcxy.com/p/14486.html

上一篇: ^ char类型提示不允许clojure defn参数

下一篇: 确定设备是否具有触摸屏