質問

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top