¿Cómo detectar botones virtuales (Android 4)?

I would like to detect if the phone running my Android application uses hardware buttons or virtual buttons (Galaxy Nexus).

¿Hay alguna forma de obtener esta información?

preguntado el 09 de marzo de 12 a las 13:03

1 Respuestas

Since API level 14 you can use ViewConfiguration.hasPermanentMenuKey () to detect if a device has a permanent menu key.

Generally, virtual and physical buttons are mutually exclusive, so this would help you. Example:

public class MyActivity extends Activity {

    // ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        boolean hasPermanentMenuKey = ViewConfiguration.get(this).hasPermanentMenuKey();
        boolean hasVirtualKeys = !hasPermanentMenuKey;
        String message = hasVirtualKeys ? "This device has virtual menu keys" : "This device has physical keys";
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }
    // ...

}

ADVERTENCIA: There are some cases in which this won't help you. For example, say a manufacturer decides to use virtual keys along with physical menu keys; then this method wouldn't help you. Another case would be if the phone has no virtual keys and no physical menu key (e.g. Samsung S5).

Respondido 07 Abr '15, 10:04

ViewConfiguration.hasPermanentMenuKey() returns false on Samsung S5 but the phone has no virtual keys. I don't think this is a proper way to check for virtual keys any longer. - tomrozb

You're right. The S5 has no physical menu key but has no virtual buttons either, so boolean hasVirtualKeys = !hasPermanentMenuKey; is no longer valid. I've updated my answer with a warning regarding this. - cristian garcia

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.