Last active
October 25, 2022 15:04
-
-
Save dseerapu/b768728b3b4ccf282c7806a3745d0347 to your computer and use it in GitHub Desktop.
Android app inactivity timeout | Android Logout timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LogOutTimerUtil { | |
public interface LogOutListener { | |
void doLogout(); | |
} | |
static Timer longTimer; | |
static final int LOGOUT_TIME = 600000; // delay in milliseconds i.e. 5 min = 300000 ms or use timeout argument | |
public static synchronized void startLogoutTimer(final Context context, final LogOutListener logOutListener) { | |
if (longTimer != null) { | |
longTimer.cancel(); | |
longTimer = null; | |
} | |
if (longTimer == null) { | |
longTimer = new Timer(); | |
longTimer.schedule(new TimerTask() { | |
public void run() { | |
cancel(); | |
longTimer = null; | |
try { | |
boolean foreGround = new ForegroundCheckTask().execute(context).get(); | |
if (foreGround) { | |
logOutListener.doLogout(); | |
} | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} catch (ExecutionException e) { | |
e.printStackTrace(); | |
} | |
} | |
}, LOGOUT_TIME); | |
} | |
} | |
public static synchronized void stopLogoutTimer() { | |
if (longTimer != null) { | |
longTimer.cancel(); | |
longTimer = null; | |
} | |
} | |
static class ForegroundCheckTask extends AsyncTask<Context, Void, Boolean> { | |
@Override | |
protected Boolean doInBackground(Context... params) { | |
final Context context = params[0].getApplicationContext(); | |
return isAppOnForeground(context); | |
} | |
private boolean isAppOnForeground(Context context) { | |
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); | |
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); | |
if (appProcesses == null) { | |
return false; | |
} | |
final String packageName = context.getPackageName(); | |
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { | |
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} | |
} | |
Use above code in Activity as below : | |
public class MainActivity extends AppCompatActivity implements LogOutTimerUtil.LogOutListener | |
{ | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
LogOutTimerUtil.startLogoutTimer(this, this); | |
Log.e(TAG, "OnStart () &&& Starting timer"); | |
} | |
@Override | |
public void onUserInteraction() { | |
super.onUserInteraction(); | |
LogOutTimerUtil.startLogoutTimer(this, this); | |
Log.e(TAG, "User interacting with screen"); | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
Log.e(TAG, "onPause()"); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
Log.e(TAG, "onResume()"); | |
} | |
/** | |
* Performing idle time logout | |
*/ | |
@Override | |
public void doLogout() { | |
// write your stuff here | |
} | |
} | |
@bolisettysiri please check this condition
https://gist.github.com/dseerapu/b768728b3b4ccf282c7806a3745d0347#file-gistfile1-txt-L68.
Above condition works only when the app is in the foreground
Please modify this logic according to your needs.
Works perfectly fine
@bolisettysiri please check this condition
https://gist.github.com/dseerapu/b768728b3b4ccf282c7806a3745d0347#file-gistfile1-txt-L68.Above condition works only when the app is in the foreground
Please modify this logic according to your needs.
Stil the same when the app is in the foreground
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi sir,
i have worked with this code,auto logout is working fine but if the screen is off or minimize auto logout not happening,can you please how to deal with this