2017-02-14 85 views
0

如果屏幕被锁定(当应用程序仍在那里)或者如果应用程序已移至背景超过5分钟,我想要注销我的应用程序。我有一个扩展了AppCompatActivity的BaseActivity。所有其他活动都扩展了BaseActvity。
我在BaseActivity中使用了下面的代码,但在5分钟后,应用程序在LoginActivity中自行打开。你能帮我解决这个问题吗?自动注销用户,当应用程序在后台5分钟,当用户恢复应用程序?

的Java代码如下:

@Override 
    protected void onPause() { 
     super.onPause(); 
     Log.v(TAG, "on pause called"); 
     timer = new Timer(); 
     Log.i(TAG, "Invoking logout timer"); 
     LogOutTimerTask logoutTimeTask = new LogOutTimerTask(); 
     timer.schedule(logoutTimeTask, 300000); //auto logout in 5 minutes 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     Log.v(TAG, "on resume called"); 
     if (timer != null) { 
      timer.cancel(); 
      Log.i(TAG, "cancel timer"); 
      timer = null; 
     } 
    } 

    private class LogOutTimerTask extends TimerTask { 

     @Override 
     public void run() { 

      //redirect user to login screen 
      Constants.SESSION_ID = ""; 
      Intent i = new Intent(getApplicationContext(), LoginActivity.class); 
      i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(i); 
      finish(); 
     } 
    } 

编辑

我添加了所有可能的建议,但是当我启动应用程序定时器工作均匀。代码片段会很有用。

+0

千万不要错过意向LoginActivity因为这将启动活动 只需设置你退出的逻辑在那里。 – user3040153

+0

使用具有待定意图的警报管理器。 –

回答

0
@Override 
protected void onPause() { 
    super.onPause(); 
    long LastTime= System.currentTimeMillis(); //save this time 
} 



@Override 
protected void onResume() { 
    super.onResume(); 
    long CurrentTime= System.currentTimeMillis(); //this is your current time 
    //get LastTime and compare 
    long difference = CurrentTime - LastTime; 
    // now convert difference into minutes and start login activity 
} 

这可能会帮助你。

0

您可以使用:

import java.util.Calendar 

Calendar c = Calendar.getInstance(); 
int seconds = c.get(Calendar.SECOND); 
int minutes = c.get(Calendar.MINUTES); 

这会给你此刻的用户的时间。

您可以将它们保存在onStop()中,并在onRestart()/ onResume(如果您销毁它)中查看它们。在那里你可以运用你的逻辑。进行一些计算,以便可以获得有关已经过去多少时间的信息。 您可以将其保存在SharedPreferences或其他您喜欢的方式。

0

这里的问题是您的LogoutTimerTask类包含自动移动到登录屏幕的代码。不要在该LogoutTimerTask类中启动一个活动。相反,设置一个全局定义的变量值,在5分钟后通过run()方法注销,当用户重新进入你的应用程序时,我建议你在onCreate中使用该变量来检查你是否手动注销了用户并从那里拨打相应的活动类别

0

我已经使用以下代码段解决了该问题。 由于我在登录后使用片段,因此仅在包含片段的Activity中使用此代码才能帮助我。

@Override 
    protected void onPause() { 
     super.onPause(); 


    } 

    private class LogOutTimerTask extends TimerTask { 

     @Override 
     public void run() { 

      //redirect user to login screen 
      finish(); 
      Constants.SESSION_ID = null; 
     } 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     timer = new Timer(); 
     Log.i(TAG, "Invoking logout timer"); 
     LogOutTimerTask logoutTimeTask = new LogOutTimerTask(); 
     timer.schedule(logoutTimeTask, DISCONNECT_TIMEOUT); //auto logout in 5 minutes 
    } 

@Override 
    public void onResume() { 
     super.onResume(); 
     if (timer != null) { 
      timer.cancel(); 
      Log.i(TAG, "cancel timer"); 
      timer = null; 
     } 
} 
相关问题