Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android auto-logout when app goes to background

Tags:

android

I want to detect when my application is sent to the background. There are many questions about hooking the HOME key - I understand this is possible only by registering as a launcher app.

...BUT... as always there is a client who wants certain behaviour...

We have an app with high security requirements. The client wants the app to log out of the server whenever the app goes into the background for whatever reason (phone call, HOME key, back on last activity) (* *to clarify I mean that when the front Activity on the screen is not one of my app's activities **).

So, if I can't hook the HOME key, what other options are there? Obviously just hooking onPause() won't help, because that is Activity-specific.

The "best" we have come up with is to keep an array of Activity references in our Application class. In each Activity's onResume() we add it to this array. In onPause() we remove it. Also in onPause() we enumerate through this array to find out if any of the registered activities are in the foreground. If no foreground activity is found, user gets logged out.

I am unhappy with this as a solution, and hope to find a better way.

like image 541
Richard Le Mesurier Avatar asked Jan 23 '12 07:01

Richard Le Mesurier


People also ask

How do I get Android to automatically logout at a certain time everyday?

just follow this, create an broadcast receiver, register to receive an intent with action say ACTION_CLEAR_SESSION. register an pending intent of type boradcast, targetting your receiver with alarm manager. set the repeat mode as daily, and set the trigger time just before midnight.

How do I keep my Android apps from logging in?

When users log in to your application, store the login status into sharedPreference and clear sharedPreference when users log out. Check every time when the user enters into the application if user status from shared Preference is true then no need to log in again otherwise direct to the login page.


2 Answers

// use service

// in that

@Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

      IntentFilter filter = new IntentFilter();
     filter.addAction(Intent.ACTION_SCREEN_OFF);
     filter.addAction(Intent.ACTION_CALL);
     filter.addAction(Intent.ACTION_ANSWER);

     registerReceiver(mIntentReceiver, filter);

}

// then in BroadcastReceiver

private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if(action.equalsIgnoreCase("android.intent.category.HOME") )
            {
//logout logic
} 
else if(action.equalsIgnoreCase("android.intent.action.SCREEN_OFF") )
            {
//logout logic
}

else if(action.equalsIgnoreCase("android.intent.action.DIAL") )
            {
//logout logic
}
else if(action.equalsIgnoreCase("android.intent.action.CALL")){
/    /logout logic
}
}
like image 103
Padma Kumar Avatar answered Sep 20 '22 10:09

Padma Kumar


We eneded up going for something based on solution here by @peceps: Run code when Android app is closed/sent to background.

like image 38
Richard Le Mesurier Avatar answered Sep 18 '22 10:09

Richard Le Mesurier