Android:检测用户不活动/检测(软键盘)键盘输入
我想在我的 Android 应用程序中检测“用户不活动”。更准确地说:我想检测用户是否在特定时间内没有与我的应用程序进行任何交互(触摸屏幕、滚动、输入文本......)。从技术上讲,我使用一个在每次(用户)交互时重置的计时器。
在我的活动中,我重写了 onUserInteraction 方法来检测诸如滚动、触摸屏幕之类的交互...
@Override
public void onUserInteraction(){
resetInactiveTimer();
}
不幸的是,当用户与软键盘交互时,不会调用 onUserInteraction。我认为原因是软键盘不是我的活动的一部分。
对于我的应用程序中的编辑文本,我使用 TextWatcher 和 onTextChanged 方法,效果很好。但我的应用程序还包含一个加载任意网页的 WebView。当然,某些网页可能包含输入字段,我不知道如何检测用户与软键盘交互来编辑这些文本字段。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
还对此感兴趣吗?
您的 Activity 实现了
KeyEvent.Callback
,因此您可以重写onKeyDown
:或者,(在最常见的情况下)如果在
EditText 中用光标按下按键
或类似的,您需要实现一个OnKeyListener
并使用onKey
方法调用resetInactiveTimer();
Still interested in this?
Your activity implements
KeyEvent.Callback
, so you can overrideonKeyDown
:Alternatively, (in the most common circumstance) if the key is pressed with the cursor in an
EditText
or similar, you will need implement anOnKeyListener
and use theonKey
method to callresetInactiveTimer();