Reputation: 2638
I have an application where you have to log in like an user with your password.
This application needs to be secure, so it needs to Log Out the user when he doesn´t tocuh the screen during a determinate period of time(let´s say, 5 minutes).
The problem is that, once the user is logged in, he can go through many activities, not only one, and I would like to know if there is a way in Android to know if the user can get Logged out after few minutes.
I was thinking about using the method onInterceptTouchEvent(MotionEvent ev), but the problem is that, ¿do I have to use this method on every activities? ¿there is another easier way to manage it in Android?
Thanks a lot!
Upvotes: 1
Views: 369
Reputation: 22064
Instead of implementing this on every Activity
, you could create one Activity
like this:
public class BaseActivity extends Activity
and implement onInterceptTouchEvent
in your BaseActivity
.
Then from all other Activities
you will have to extend the BaseActivity
UPDATE:
You will have to use onTouch
if you want to listen for events in whole screen. This requires that in every xml file of your Activity
, you will have to give an id
for the root view of your xml (Linearlayout
, Relativelayout
, Framelayout
or whatever). So in order to let your BaseActivity
get inputs from other Activities
you will have to pass your root view to it, like this:
public class MyActivity extends BaseActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
super.setRootView(findViewById(R.id.rootView));
}
}
public class BaseActivity extends Activity implements OnTouchListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void setRootView(View rootView){
rootView.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event) {
Log.d("BaseActivity", "onTouch!");
return super.onTouchEvent(event);
}
}
Upvotes: 1
Reputation: 1497
1. One way to achieve this would be to get current time (using System.currentTimeMillis()
) as the user logs in to the application. Let's save this in a variable t1
.
2. Start a service which will monitor/check/execute below things:
2a. whenever user touches any of your activities set the value of t1
to current time.
2b. continuously get the current time (t2
) and calculate the time-lapse between t1
and t2
(i.e. t2 - t1
). If this time-lapse is equal to or greater than say 5 mins you can call the logOut()
function.
Yes, you would have to inform the background service that user is present, from every activity. Although solution provided by Carnal would go a long way in reducing your effort to achieve this.
Upvotes: 1