Android: Detect user inactivity / Detect (softkeyboard) keyboard input
Asked Answered
F

1

10

I want to detect "user inactivity" in my Android app. To be more precise: I want to detect if the user has NOT done any interaction with my app (touching the screen, scrolling, input texts ...) for a specific time. Technically I use a timer that is reseted on each (user) interaction.

In my activity, I override the onUserInteraction method to detect interactions like scrolling, touching the screen ...

@Override
public void onUserInteraction(){
    resetInactiveTimer();
}

Unfortunately, onUserInteraction is not called when the user interacts with the soft keyboard. I think the reason is, that the soft keyboard is not part of my Activity.

For the edit texts in my app I use TextWatcher and the onTextChanged method which works fine. But my app also contain a WebView that loads arbitrary web pages. Of course some web pages could contain input fields and I do not know how to detect that the user interacts with the soft keyboard to edit those text fields.

Fascia answered 14/1, 2011 at 10:36 Comment(1)
u got any solution for this?Swampland
H
1

Still interested in this?

Your activity implements KeyEvent.Callback, so you can override onKeyDown:

@Override
public boolean onKeyDown (int keyCode, KeyEvent event) {
    resetInactiveTimer();
    return false;
}

Alternatively, (in the most common circumstance) if the key is pressed with the cursor in an EditText or similar, you will need implement an OnKeyListener and use the onKey method to call resetInactiveTimer();

Hadden answered 8/5, 2011 at 16:31 Comment(3)
Input Methods are not required to sent key events. In fact jelly beans softkeyboard does not send any key event when backspace is pressed.Simulant
But here issue is that onUserInteraction is not called when you are typing in edittext. Also if I want to use onKeyDown or onKey, I have to set key press listener to each and every edittext. Is there any general option where I can track any keypress event from soft keyboard ?Awoke
Not working. Activity's onKeyDown isn't invoked when I type smth in edit textSegovia

© 2022 - 2024 — McMap. All rights reserved.