Timeout screen if user is inactive in an Android application - android

I would like to be able to detect if a user is inactive in my Android application for a specific period of time, if so I want to start a new activity where the user is asked for a password to resume using the application. The previous activity should start off where it was left.
Is this possible in an Android application? I am guessing I should start off a separate thread as soon as the application loads, which keeps checking for user activity, but how do I do that.

This is a very difficult question to answer without knowing details of your Application.
For example...how many Activities does the app have? What do those Activities do? What widgets (buttons, listviews etc etc) does each have?
For a start, to gauge whether a user is 'active' or not then you'd need to keep a 'last used' time field. Doing this however would mean keeping a static 'long' variable (either at the Application level or persisted through SharedPreferences if there is more than one Activity). The variable would hold the 'last used' time in milliseconds.
You would need to update this every time the user interacted with any Activity (touched, scrolled, clicked a button etc). Those interactions require different listeners which would each have to update the 'last used' time. More than that, you'd need each listener to test the 'last used' variable before updating it with the current time - if it was longer ago than 5 minutes (or whatever your timeout) then they'd need to raise an event to force a popup requiring the user enters the password.
This is all doable but it's complex.
Alternatively, you could simply set a countdown timer but you'd still need every touch, click or gesture to reset the countdown and it would still require doing this for every Activity in your app. Pretty much most of the above also applies.
Describing your app further might help someone provide a more concise answer.

Related

Android Talkback announcement interrupted

I have activity A with recipes that opens activity B for more options for a recipe.
In activity B I'm doing an action (like adding to favorites) that finishes activity B.
When doing that action, I'm triggering a Talkback announcement so that the user knows the action has been completed successfully.
However, the announcement gets interrupted halfway through because activity B is finishing and Talkback starts announcing activity A instead.
How can I make sure that Talkback announcements are not interrupted? Is there any way to change the priority in the API (similar to live regions?)
I also tried adding a Toast, but the toast announcement gets interrupted as well...
Any suggestions?
Thanks!
I have run into issues like these as well. I did not find a way to stop interruptions, but generally use the following approaches to get around the issues:
Keeping announcements short whenever possible. "Item With A Long Name has been saved to your favorites list" is too long, at that point user has a lot of context and has an expectation of what is going to happen, so short announcement is usually fine. Something like "Favorited"/"Saved" is to the point and takes way less time to be announced! This is important with translations as well, some languages are so much longer than others.
Used carefully and only very infrequently: Adding a delay. Send announcement, set a timer for 500ms or whatever it needs to be, accounting for translations lengths too, finish activity after that. This is something that ideally will not need to be used a lot: only for very important announcements which shouldn't be missed! Having delays could badly affect user experience, so for me this is the last strategy.

android: run timer for long period

if i want to run a timer for a long time in android and show the user, when they go to a particular activity, for example, the duration since the timer started as a live number - how should I implement this? By live, i mean the time shown changes in real time as one would expect a timer to do. There will only need to be one instance of this timer, it'll pretty much be like android's stopwatch but implemented to function within a custom app.
Would I require wakelock?
should I create a service?
or should I just use a simple java timer?
any help/advice much appreciated.
thank you.
What do you mean 'long timer' 1 minut, day?
You have to realise that android can kill you activity any time, so the activity is not the right place to do it.
You can run you operation in Service - his life is longer then activity's, but you probably want to check time even if user returns in activity after week or reboot device.
If you tell more info about what you want it'll be easier to solve you problem.
show the user, when they go to a particular activity
As I see you problem the solution should be like this:
First enter to the activity - create timer, start it,
On activity stop save current value of the timer and system time,
On recreate activity read saved value and start new timer with value of init_value + (current_time - saved_time).
In this case you can be sure that timer is persistant even if user left your activity and even restart device.
And also battery life will be much more longer :)

Android Best Way to Detect and Handle User INACTIVITY

Inactivity is a very important EVENT. For many apps if the user does not interact with it for a certain number of seconds its time to reset the app and go back to main activity logout, or conserve power. So I would really like to get some feedback on the best way to detect this. In fact I think everyone will benefit from a good solution to this.
So my question is twofold:
1) Is there a better way to detect user inactivity than using a combination of
activity.onUserInteraction() to reset a CountDownTimer?
Note: One reported downside to this approach is that softkeypad interaction might not be
caught by this approach.
Note: Another reported downside is the CountDownTimer is off main thread and might not update
correctly. I am not sure how big an issue this is?
Note: CountDownTimer appears to have cancellation issues as well:
how to stop/cancel android CountDownTimer
2) Lets say that onUserInteraction()/CountDownTimer is the best/only solution to this problem
there are still some questions:
a) should each activity launch its own countdown timer?
b) Should a single countdown timer be restarted in the onCreate method of each activity?
c) lets say I want to dim the screen or goto main activity when the countdown expires where
should the timeout handler be located? In each activity? In a service?
Thanks
Just stumbled upon this question as I've answered something similar just now.
Personally, I'd opt for option 2 that you have suggested, and put a timer into a singleton so its available across all activities. Theres no need for a separate countdown timer unless you have a specific requirement to react different under different features of your application.
Why would you want to reset the timer in the onCreate? You should do that each time the user interacts with the application, such as in the activity.onUserInteraction() method.
To quote from my previous answer:
You'll need to invest a little thought into exactly what your
requirements are here, but from what I can tell, you want to keep
track of the user interactions and if a time limit expires since the
last interaction, perform some action, in your case logging them out
of your application.
Firstly, you'll need some place that you can track when the last
interaction occured, since you'll want this to be application wide you
could use a singleton to hold this, or override the Application class,
either way should do.
Next, you'll need to start tracking user interactions. From your
activities, you can override the onUserInteraction method, this gets
invoked anytime the user interacts with the application such as key
event. Each time you hit this method, update your singleton and let it
know something has happened, with a timestamp.
Finally, you'll need some kind of looping check to constantly check if
anything has happened recently. Theres various was of doing this, you
could have a continuous loop that compares current timestamp to the
last recorded event, a bit of draft code :
while(true)
{
if (timeLastEventRecorded < (now - 15))
{
//nothing has happened in 15 minutes, so take corrective action
}
}
Presumably you'll already have some code in your application that
takes care of logouts, such as when the user clicks "logout", you
should just be able to invoke that in the sample above.

Android. how can I determine how long a user has been inactive?

I have a wizard app where user goes thru 8-10 activities but might drop off at any point. I need to detect user inactivity somehow, which could occur on any of the activities. What is the best way to capture and detect his. Thanks
I'd persist (using one of the recommended methods, ie SharedPreferences, SQLite, file etc) a timestamp and activity identifier (so you know which one they dropped off on) that you can grab later. Depending on how you define "inactivity" this should probably be set in either onPause(), onStop(), or onDestroy().
Full activity lifecycle link: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

Require login every time user returns to Android application

I am working on an Android app that deals with sensitive user information. One of the requirements is that the user is required to log back into the application whenever they leave it and come back. This is easily dealt with for the case when the user presses the Home button and then relaunches the app (android:clearTaskOnLaunch attribute on the Activity in AndroidManifest.xml). However, we need to do the same thing when the user long presses the Home button, switches to another application, then comes back.
I have researched this every way that I can think of and have not found a workable solution. Is this even possible with Android?
When answering, please keep in mind that this is a business requirement which I have no control over.
Well, I had the same problem yesterday. This is what I did and it works fine:
Added android:launchMode="singleTask" to the main activity in the AndroidManifest.xml
Called my boss and say: ey, this is going to take a long while... hold on!
Went and drank beer all night.
Just to clarify, my main activity only has a button that says login and launches the login page.
What have you tried? You can always clear whatever session you are saving in the proper Activity lifecycle method.
If I understand you correctly that you want to require an authorisation every time someone backs into the app, whether afresh or coming back to it, then you can override the onRestart() activity lifecycle event on the activity (or activities). So in onRestart() you can redirect the user to the login screen (you may also wish to consider onResume() depending on your requirements)
The lifecycle chart on this page will make this clearer:
http://developer.android.com/reference/android/app/Activity.html
Would it be possible to make it a time based thing, rather than strictly left the app and returned?
You could have a separate service that keeps track of when the last time the user accessed the application was.
I.e, in each onPause the Activity tells the service that an Activity was paused. The service records the time of that.
In each onResume, the Activity informs the Service that it wishes to resume. If some amount of time has passed since the last onPause, then the Service indicates that a login is required.
I think this would make a nicer user experience than just every time they leave the app. That could be very frustrating, to take 30 seconds to read a text, and then have to sign in again.
I suppose if you tweak it to have the timeout be very short, it has very similar behavior to what you requested anyways, but with the option of making it less draconion.
I think the easiest way to implement this, would be to add a field to your main activity like private boolean isLocked = true;.
To lock the app when another one is shown, set isLocked = true in the onPause() method. To make sure, that your don't lock your app, when returning from your own activities, start them via startActivityForResult() and unlock it in onActivityResult.
You can now check in onResume() wether your app is locked and redirect the user to your login screen.

Categories

Resources