安卓,X分钟后自动登出

时间:2021-10-09 05:26:56

I'm looking to implement a feature that logs out the user after X minutes of inactivity. After going through a similar answer on SO, the suggested method seems to be to -

我希望实现一个特性,在不活动X分钟后将用户注销。经过类似的回答后,建议的方法似乎是-

  • Have a timer running in the background. Schedule it to timeout after x minutes.
  • 在后台运行计时器。把它安排在x分钟后暂停。
  • In every function where the user interacts with the app (basically all event handlers), call a method that resets the timer.
  • 在用户与应用程序交互的每个函数(基本上都是事件处理程序)中,调用一个重置计时器的方法。

I can't think of anything better than this myself, but it's seems to be a huge pain even for a medium sized application that has 6-7 different screens and a whole bunch of UI components. Is there a better way to handle this?

我想不出有什么比这更好的了,但对于一个拥有6-7个不同屏幕和一大堆UI组件的中型应用来说,这似乎是一个巨大的痛苦。有更好的方法来处理这个问题吗?

Thanks,
Teja.

谢谢,Teja。

4 个解决方案

#1


2  

Have a timer running in the background. Schedule it to timeout after x minutes.

在后台运行计时器。把它安排在x分钟后暂停。

No and yes. Use a timer if you're implementing it in a Service or in an IntentService. Otherwise, don't.

没有,是的。如果在服务或IntentService中实现计时器,请使用计时器。否则,不。

In every function where the user interacts with the app (basically all event handlers), call a method that resets the timer.

在用户与应用程序交互的每个函数(基本上都是事件处理程序)中,调用一个重置计时器的方法。

That solution would be hard to maintain.

这个解决方案很难维持。

You should have an IntentService (demonstrating article here) running in the background that can easily implement a TimerTask or a Handler and make the runnable code inside it fire a broadcast to your activities. In your activities you can easily add a BroadcastReciever and in that case you can log out the user if time is out. You can start your service when your application isn't visible to the user.

您应该在后台运行一个IntentService(在这里演示文章),它可以轻松地实现TimerTask或处理程序,并使其中的runnable代码向您的活动发出广播。在您的活动中,您可以很容易地添加一个BroadcastReciever,在这种情况下,如果超时,您可以注销该用户。当您的应用程序对用户不可见时,您可以启动您的服务。

#2


5  

You can use a CountDownTimer and restart it from onUserInteraction() in every Activity()

您可以在每个活动()中使用CountDownTimer并从onUserInteraction()中重新启动它

#3


1  

This is how I would go about this:

我是这样说的:

1) Create a global variable to represent a time log

1)创建一个全局变量来表示时间日志

2) During the onStop call for each activity, update the global variable with the current time.

2)在每个活动的onStop调用期间,使用当前时间更新全局变量。

3) During the onResume call for each activity, compare the current time with the global variable time to see how much time has passed

3)在每个活动的onResume调用期间,将当前时间与全局变量时间进行比较,以查看已通过了多少时间。

#4


1  

This is something that I wanted to implement for myself. Here is a "library" I made: https://github.com/zoltanersek/android-timeout-activity

这是我想为自己实现的。这是我做的一个“库”:https://github.com/zoltanersek/android-timeout活动

Usage:

用法:

public class TestActivity extends TimeoutActivity {

@Override protected void onTimeout() {
 // logout
}

@Override protected long getTimeoutInSeconds() {
  return 15 * 60; // 15 minutes
}

}

#1


2  

Have a timer running in the background. Schedule it to timeout after x minutes.

在后台运行计时器。把它安排在x分钟后暂停。

No and yes. Use a timer if you're implementing it in a Service or in an IntentService. Otherwise, don't.

没有,是的。如果在服务或IntentService中实现计时器,请使用计时器。否则,不。

In every function where the user interacts with the app (basically all event handlers), call a method that resets the timer.

在用户与应用程序交互的每个函数(基本上都是事件处理程序)中,调用一个重置计时器的方法。

That solution would be hard to maintain.

这个解决方案很难维持。

You should have an IntentService (demonstrating article here) running in the background that can easily implement a TimerTask or a Handler and make the runnable code inside it fire a broadcast to your activities. In your activities you can easily add a BroadcastReciever and in that case you can log out the user if time is out. You can start your service when your application isn't visible to the user.

您应该在后台运行一个IntentService(在这里演示文章),它可以轻松地实现TimerTask或处理程序,并使其中的runnable代码向您的活动发出广播。在您的活动中,您可以很容易地添加一个BroadcastReciever,在这种情况下,如果超时,您可以注销该用户。当您的应用程序对用户不可见时,您可以启动您的服务。

#2


5  

You can use a CountDownTimer and restart it from onUserInteraction() in every Activity()

您可以在每个活动()中使用CountDownTimer并从onUserInteraction()中重新启动它

#3


1  

This is how I would go about this:

我是这样说的:

1) Create a global variable to represent a time log

1)创建一个全局变量来表示时间日志

2) During the onStop call for each activity, update the global variable with the current time.

2)在每个活动的onStop调用期间,使用当前时间更新全局变量。

3) During the onResume call for each activity, compare the current time with the global variable time to see how much time has passed

3)在每个活动的onResume调用期间,将当前时间与全局变量时间进行比较,以查看已通过了多少时间。

#4


1  

This is something that I wanted to implement for myself. Here is a "library" I made: https://github.com/zoltanersek/android-timeout-activity

这是我想为自己实现的。这是我做的一个“库”:https://github.com/zoltanersek/android-timeout活动

Usage:

用法:

public class TestActivity extends TimeoutActivity {

@Override protected void onTimeout() {
 // logout
}

@Override protected long getTimeoutInSeconds() {
  return 15 * 60; // 15 minutes
}

}