简体   繁体   English

如何在特定时间后退出Android应用程序?

[英]How to logout of android app after particular time?

I'm building an android app which has a login and logout option. 我正在构建一个具有登录和注销选项的Android应用程序。 How do I make the app go back to login screen if the user has logged in and not used it(or kept idle) for some time(say 10min)? 如果用户已登录并且未使用(或保持闲置)一段时间(比如10分钟),如何让应用程序返回登录屏幕? Also, how do I check for multiple logins? 另外,如何检查多次登录? Please note that my app uses an external database(mysql). 请注意我的应用程序使用外部数据库(mysql)。 I did this using php code. 我是用PHP代码做的。 Also, how do I check for multiple logins(I've multiple users) from mysql and allow each user to log in? 另外,如何从mysql检查多个登录(我有多个用户)并允许每个用户登录?

This is my code for authentication: 这是我的身份验证代码:

try
{
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://10.0.2.2/user_validate.php");
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    InputStream inputStream = entity.getContent();

    byte[] data = new byte[256];

    buffer = new StringBuffer();
    int len = 0;
    while (-1 != (len = inputStream.read(data)) )
    {
        buffer.append(new String(data, 0, len));
    }

    inputStream.close();
}
catch (Exception e)
{
    Toast.makeText(Login.this, "error"+e.toString(), Toast.LENGTH_LONG).show();
}

if(buffer.charAt(0)=='Y')
{
    Intent intent = new Intent(getApplicationContext(), 
        Home.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    finish();
    startActivity(intent);
}

In the app or on the server you want to store the timestamp of the last successfully completed user action (that requires login credentials). 在应用程序或服务器上,您要存储上次成功完成的用户操作的时间戳(需要登录凭据)。

Then test against that timestamp to see if the current request is more or less than the 10 min threshold. 然后针对该时间戳进行测试,以查看当前请求是否大于或小于10分钟阈值。 If its longer than 10 mins after the last successful timestamp then you need to get the user to log in again. 如果在最后一个成功的时间戳之后超过10分钟,那么您需要让用户再次登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM