I have an application which needs to log the user out after no activity is detected. I am using a timer that resets when the user interacts with the app. I have been using OnUserInteraction method; however this does not track interaction with the Soft Keyboard.
#Override
public void onUserInteraction() {
super.onUserInteraction();
logoutTimer.RestartTimer();
}
My problem is that this application allows the user to input text (without limit) and the application times the user out in the middle of inputting text. Is there another way of listening for Keys being pressed on a softkeyboard? (I know I can create a listener on the editText but there are multiple editTexts on different fragments and was wondering if there is a better way)
I guess there is no way to do this via onUserInteraction only. You can extend EditText, set a TextWatcher in it and catch all interactions with this EditText in TextWatcher's callbacks. Then you have to substitute this newly created EditText of yours in all places where you use to have standard EditText. I tried to override onKeyEvent() method inside the activity which hosts my EditText and refresh the timer in onKeyDown or onKeyUp, but to no avail. SoftInput seems to redirect all touches not to your activity, but to its own Window, that is why you won't see any keyEvents in activity.
Related
I create a keyboard app.
It changes to another by the functionality when it is fulfilled with specific situation.
This method is that switchInputMethod() which inherits InputMethodService class. And I tried to change the keyboard temporary.
However, when I call switchInputMethod(), it behaves as changing “Default” App of Android device.
After that, I can’t show my keyboard app.
In this case, I reselect “Default” of “language ant input” from “Setting” in my device.
I’d like to realize the following.
There is Activity has EditText1, EditText2 and EditText3.
In the case of EditText1 and EditText3, I input with my keyboard app.
And EditText2, I do with another.
[[add image]]
If its your app that's running, you can override the default input method for a given field via android:inputMethod in xml on the TextView or setInputMethod in Java. Then it would use that input method for the TextView and return to default for others.
I have a condition when a specific event will occur in background, I have to show a Floating Action Button in bottom of activity to notify the user if App in fore ground. No matter in what activity the user is, if that event trigger by a service, I have to show a FAB not a dialog, I mean user can interact with activity and FAB both at the same time. How could I achieve this if I do not wanna repeat the same code in all activities of my app.
You can make a utill class and make a static method in that class so in which activity you want to access that method just Classname.methodname and send parameter.
or you can use that method in that Activity and extend from that activity
You can create a simple activity that matches your need and make all other activities derived (inherited) from that one.
The following method:KeyChain.choosePrivateKeyAlias creates system dialog which allows user to choose a key alias from a list. As one of the arguments it takes Activity context which will be used to spawn this dialog. When selection is made it will call a callback.
In my scenario user doesn't select any alias but presses "Home" button. I would like to intercept this event. How can I do that?
Also I would like to cancel this dialog programatically. In this case I need some way to access this child Activity (please note that choosePrivateKeyAlias doesn't return any "handle" to the new dialog). Is it possible to access child Activity without any references (handle/id/etc.) to it?
There is no way to programatically end that activity because it is a system activity. It's the same as launching the browser on your device or the contact list. The only way to exit it is to press back and it will close that activity and resume yours.
According to my understanding, if you want to dismiss that dialog. Then as far as I know about Android OS. There are lifecycle methods for Activity. So I think
IF YOU CALL "yourDailog.dismiss()" INSIDE YOUR ACTIVITY's "onStop()". SO YOUR PROBLEM WILL BE SOLVED
If you do it in above way, so whenever you press HOME button, It will call onStop method of your activity. At that time, It will dismiss that dialog.
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.
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();
I am using a simple EditText and register an View.OnKeyListener. Some GUI changes should happen (fading in/out of views) when certain conditions for the EditView text apply.
In the emulator, this works as expected. On the Motorola Droid/Milestone, the listener does not work, only
after pressing DEL, the listener is called the first time and always afterwards
when activating the number input, the listener works correctly
Note: I deactivated the suggestions with TYPE_TEXT_FLAG_NO_SUGGESTIONS and used the IME action IME_ACTION_GO to have a workaround for this (otherwise the user could not "start" a request).
Found a better and working way to implement this: use EditText.addTextChangedListener with a TextWatcher class (and implement TextWatcher.onTextChanged(CharSequence s, int start, int before, int count)), works perfectly also on the Milestone/Droid.
TextWatcher cannot fully replace onKeyListener. For example, if your textbox is empty, how would you detect that user pressed DEL key? Or, what if you don't have textbox at all and you want to detect key events?