Physical home button press can be detected easily on most of the devices:
...
public class ExampleAccessibilityService extends AccessibilityService {
...
@Override
protected boolean onKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KEYCODE_HOME && event.getAction() == ACTION_DOWN)
Log.d("Example", "The home key is pressed.");
return super.onKeyEvent(event);
}
}
But the code above doesn't work on some devices that have a pressure-sensitive virtual home button. I suppose these Samsung Galaxy devices are affected: S8, S8+, S9, S9+, Note10, Note10+ and Fold. Officially, it is considered a pressure sensor, not a button.
How to read this sensor?
The TYPE_PRESSURE
sensor event is related to the barometer, it shows the ambient air pressure in hPa or mbar.
The getPressure()
method returns "the size of the capacitive object rather than literal pressure".
I don't need info about the level of pressure, I just want to know if the pressure-sensitive virtual home button is pressed.