创建和处理一个应用程序超时的Andr​​oid [英] Creating and handling an app timeout in Android

查看:172
本文介绍了创建和处理一个应用程序超时的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是处理应用程序超时,如贝宝的最佳途径。我希望用户选择1,5或15分钟的超时期限,因此,当他们打开的应用程序,他们将不得不重新登录。

I was wondering what would be the best way to handle an application timeout, such as PayPal. I want the user to select between 1, 5, or 15 minute timeout period, so when they open up the application they would have to log in again.

我的onResume方法:

My onResume method:

@Override
    public void onResume() {

    } 

它什么也没有。但它崩溃。

It has nothing in it. But it crashes.

作为一个说明:我的应用程序有两个活动。第一个活动是一个登录界面。第二个活动是主屏幕。所述onResume方法是在第二活动

As a note: my app has two activities. The first activity is a login screen. The second activity is the main screen. The onResume method is in the second activity.

推荐答案

我在我的应用程序之一这样做:

I did this in one of my apps:

您需要一个基地活动为其所有的活动将扩展而来。在此基础上的活动,添加跟踪'最后用户活动时间戳的变量。在我的情况下,用户的活动只是意味着他们触摸屏幕。所以覆盖dispatchTouchEvent(MotionEvent EV)的方法,并设定最后一个用户的活动,以当前的时间戳。

You need a base Activity for which all of your activities will extend from. In this base activity, add a variable that keeps track of the 'last user activity' timestamp. In my case, user activity simply means they touch the screen. So override the dispatchTouchEvent(MotionEvent ev) method, and set the 'last user activity' to current timestamp.

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    lastActivity = new Date().getTime();
    return super.dispatchTouchEvent(ev);
}

那么这个基地活动的onResume()方法,只是比较当前时间戳与最后用户活动时间戳。如果它比是1,5或15分钟(用户可配置),然后启动另一项活动要求用户登录。

Then in onResume() method of this base activity, just compare current timestamp with 'last user activity' timestamp. If it more than either 1, 5 or 15 minutes (configurable by user), then launch another activity to ask the user to login.

@Override
public void onResume() {
    long now = new Date().getTime();
    if (now - lastActivity > xxxx) {
       // startActivity and force logon
    }
} 

这篇关于创建和处理一个应用程序超时的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆