0

I need to logout from my application if it goes into background and is resumed after more than 15 minutes.

Please refer to the code after implementing the solution provided

public class BaseActivity extends Activity {
    BaseActivity context;
    private AlarmManager alarmMgr;
    private PendingIntent alarmIntent;

    BaseActivity() {
        context = this;
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (alarmMgr != null) {
            alarmMgr.cancel(alarmIntent);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(BaseActivity.this, SampleBootReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

        alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 1 * 30 * 1000, alarmIntent); // 15

    }

    class SampleBootReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "alarm", Toast.LENGTH_SHORT).show();
            Intent intent_login = new Intent(context, SignIn.class);
            intent_login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent_login);
            overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
        }
    }
}
Alex McMillan
  • 17,096
  • 12
  • 55
  • 88
Aditya Arora
  • 45
  • 1
  • 8

4 Answers4

0

You could store the time (System.currentTimeMillis) in the onPause() methode of your activity (for example using SharedPreferences). In the onResume-Methode, you could check this time and log the user of if 1000*60*15 millis are over.

KCD
  • 678
  • 5
  • 20
  • I don't want to check in all activities – Aditya Arora Apr 16 '14 at 10:38
  • You could create a Base activity with the check and inherit all other activities from that. – KCD Apr 16 '14 at 10:44
  • Keep in mind that every task-killing action by the user will stop any `AlarmManager` or background task. So if you prefer something like this, you should store the login-state only in RAM to ensure that itis resetted when you application gets killed. – KCD Apr 16 '14 at 11:16
0

You can use AlarmManager to schedule an alarm in 15 minutes from now, and then when the alarm is launched you can logout from your app. Then when you open again your app you have to sure that your cancel the current alarm thus you can assure that the logout will not be executed if the user open the app in a period of 15 minutes. You can find a full example here The good point about this aproach is that your application must not be running.

Summarizing, for the copy&paste lovers:

To schedule an alarm:

private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
    SystemClock.elapsedRealtime() +
    15 * 60 * 1000, alarmIntent); // 15 minutes

Your BroadcastReceiver that will logout from your system:

public class SampleBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // LOGOUT
    }
}

To cancel an exiting alarm:

if (alarmMgr!= null) {
    alarmMgr.cancel(alarmIntent);
}
icastell
  • 3,495
  • 1
  • 19
  • 27
  • When do i need to schedule the alarm, on login? – Aditya Arora Apr 16 '14 at 10:39
  • No, in your onStop() method from your Activity. And if you have more than once, to avoid repeating code you can do a super class and the other Activities can extending from it. And in your onStart() method you have to cancel the alarm. – icastell Apr 16 '14 at 10:45
  • Which class should be the alarm receiver and bootreceiver? – Aditya Arora Apr 16 '14 at 12:23
  • the onstop is called when i start the login another activity – Aditya Arora Apr 16 '14 at 13:25
  • In onReceive from BroadcastReceiver you should do a LOGOUT no a login, don't you? – icastell Apr 16 '14 at 13:28
  • @AdityaArora yes, but the onStart in the other Activity will cancel the alarm. Anyways you should schedule the alarm only when the user send the app to background. You can see this [question](http://stackoverflow.com/questions/4414171/how-to-detect-when-an-android-app-goes-to-the-background-and-come-back-to-the-fo) to know when an app goes to background. – icastell Apr 16 '14 at 13:29
  • How do I logout? I am sending the user to the login screen – Aditya Arora Apr 16 '14 at 16:22
0

Try following code

//Class level variable 
private boolean inBackground = false;

@Override
public void onResume()
{
     inBackground = false;
}


@Override 
public void onPause()
{
    inBackground = true;
    new CountDownTimer( 900000 , 1000 ) 
    {
         public void onTick(long millisUntilFinished) {}

         public void onFinish() 
         {
             if ( inBackground )
             {
                  // code to logout
             }
         }
    }.start();
}

Whenever your application goes into background be default control will be in onPause() method. Here I have use a CountDownTimer which will perform logout operation after 15 minute's interval.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • How do you cancel an existing timer if the user open the app in less a 15 minutes? What happen if your Activity is destroyed and you lose the timer reference? – icastell Apr 16 '14 at 10:38
  • You can do it in smart way, think your self. How many activities you have in your application ? – Lucifer Apr 16 '14 at 10:40
  • I think that you can have problems if your Activity is destroyed and launched again. Because they will have two references, don't? – icastell Apr 16 '14 at 10:42
  • `inBackground` is normal variable not a static one, so if the activity gets destroy then this variable will also get destroy. When new activity will create this variable will create again. – Lucifer Apr 16 '14 at 10:44
  • What if I need to track user inactivity when the app is in foreground – Aditya Arora Apr 16 '14 at 10:49
  • Exact! So your Activity is destroyed, and the app is launched in 8 minutes, how can you cancel an scheduled timer? The value of the background variable will be false, because they are different instances. – icastell Apr 16 '14 at 10:50
  • @icastell if the activity is destroyed then CountDownTimer will also gets destroy. – Lucifer Apr 16 '14 at 10:51
  • @AdityaArora, that's an different question. – Lucifer Apr 16 '14 at 10:52
  • @Kedarnath the coundDownTimer will not be destroyed. Who is destroying it? Nobody – icastell Apr 16 '14 at 10:52
  • @Kedarnath Sorry I mean, the timer will not be cancelled. Destroyed doesn't mean that the timer will be stopped, moreover the Android Operating System only will free the references if there are some. – icastell Apr 16 '14 at 11:03
  • @icastell, I need to double check it. will update you. – Lucifer Apr 16 '14 at 11:05
  • @AdityaArora, that why I wrote this [comment](http://stackoverflow.com/questions/23106511/android-logout-if-application-goes-in-background-for-15-minutes/23106913?noredirect=1#comment35319834_23106511) since we can not assume your code. How can I assume, what code you using for logout. A popup or something else. – Lucifer Apr 16 '14 at 11:12
  • You suppose to post this code in your question, sir – Lucifer Apr 16 '14 at 11:24
  • Also how to cancel this timer if user opens before 15 minutes? The timer should restart – Aditya Arora Apr 16 '14 at 11:30
  • If the activity comes in foreground before 15 minutes then if condition will not allow to logout. :) – Lucifer Apr 16 '14 at 11:32
  • Does this mean that everytime the activity goes in onpause the timer is restarted? And the previous instances of timer are still working – Aditya Arora Apr 16 '14 at 11:37
  • 1
    @AdityaArora, I think yes. This will happen no matter which approch you use. CountDownTimer or AlarmManager. If you keep switching your application from background to foreground, it will keep restarting. You may write a code to cancel the CountDownTimer in `onResume()` method. – Lucifer Apr 16 '14 at 11:38
  • I have multiple activities so I dont want to do the code in onPause rather I need to do when app goes into background. – Aditya Arora Apr 16 '14 at 11:38
  • my code is not working as expected. It logs out if the old timer instance has finished and the new timer instance still has not completed 15 minutes – Aditya Arora Apr 16 '14 at 11:42
  • @Kedarnath How can I refer to the CountDownTimer instance in onResume()? – Aditya Arora Apr 16 '14 at 11:45
  • `CountDownTimer countDownTimer = new CountDownTimer();` – Lucifer Apr 16 '14 at 11:46
  • everytime I start a CountDownTimer countDownTimer = new CountDownTimer(); countDownTimer reference will point to the last instance of timer. I need to cancel the old instance – Aditya Arora Apr 16 '14 at 11:46
  • `CountDownTimer countDownTimer` declare this as class level. – Lucifer Apr 16 '14 at 11:47
  • Yeah exactly, so the classlevel countDownTimer will refer to the last instance created. It will cancel the recent timer. But When I resume my activity, the old timer has already completed and set my isStatePersistant to false already. So when I check isStatePersistant this has already logged out my app – Aditya Arora Apr 16 '14 at 11:54
  • back to the same Intelligent question asked long back by @icastell - Exact! So your Activity is destroyed, and the app is launched in 8 minutes, how can you cancel an scheduled timer? The value of the background variable will be false, because they are different instances. – – Aditya Arora Apr 16 '14 at 11:56
  • When an application gets destroy , obviously all the variable gets destroy too. – Lucifer Apr 16 '14 at 11:59
  • The question is when the old timer has already finished working, the boolean isStatePersistant will be set to false and when I check it in onResume it will log me out. Whereas the current timer is still running. How can I attach the boolean variable to the timer. so that when I cancel the old timer in onResume, the boolean is also reset – Aditya Arora Apr 16 '14 at 12:04
  • @AdityaArora Are you tried my approach? I'm pretty sure that you will find less complications. Because you cannot cancel the timer unless you use some variable in your preferences indicating when the app is executing or not, but I think this i less elegant. – icastell Apr 16 '14 at 12:13
  • @icastell: what approach should I follow? – Aditya Arora Apr 16 '14 at 12:17
  • @AdityaArora, he means, have you tried his answer ? – Lucifer Apr 16 '14 at 12:18
0

Try to learn more about the activity lifecycle which is availalbe at http://developer.android.com/training/basics/activity-lifecycle/index.html . Your app is going to background which means your current activity is going into the onpause() state or onStop(). You need to do the coding in these activity cycles. In this state you can call the alarm Manager which can perform a specific task at a given Time like in your case start the app after 15 minutes

coderVishal
  • 8,369
  • 3
  • 35
  • 56