Android Force Close Application : Session Timeout


Recently we started development for an app at Truiton , in which we had to maintain sessions locally, the situation was, that if we minimize the app by tapping home key or any other key, the app’s session should log out automatically after  10 minutes. Hence I had to figure out a way in Android to Force Close Application : Session Timeout.

Now the question arises how do I close my app/session programmatically ?

The task at hand is, we do not have to force close application: session timeout immediately as user minimizes the app. Hence the first approach that came into my mind was to schedule a task to force close application:session timeout in Android. We did this by AlarmManager class of android. With the help of AlarmManager we called a BroadcastReceiver which sent the final broadcast via sendBroadcast method.

Here it goes, we’ll start by creating the layout for our main activity.

After creating this layout out screen should look something like this:

Login - Android Force Close Application-Session Timeout Tutorial

Login – Android Force Close Application-Session Timeout Tutorial

Code for MainActivity:

Here we have simply created an activity which listens to an OnClick event of a button via OnClickListener and starts the SecondActivity. Also this MainActivity receives the result and shows the message accordingly when app is force closed/session timeout.

Code for SecondActivity:

This SecondActivity is the activity where magic happens. This activity has an onReceive method which is used to receive the broadcast which will be sent from the next class i.e MyScheduledReceiver. This helps in force close application:session timeout on Android. Please define this method in all of the activities which you want to close when session expires (Highlighted Lines) this is the method which finish() is called to end the activity. Have a look at the onPause method if this class, here we are scheduling an alarm of 10 seconds with the help of AlarmManagerIntentPendingIntent and Calendar classes.

Code for MyScheduledReceiver :

In Android to force close application:session timeout, MyScheduledReceiver class is used. Although code for this class is self explanatory, here we are sending a broadcast via sendBroadcast method in onReceive method of receiver. The broadcast intent fired here has an action truiton.ACTION_FINISH which is used to finish the SecondActivity.

This is BroadcastReceiver class therefore we also need to register it as a receiver in manifest

Now you should have something like this

To sum up – In this Android Force Close Application-Session Timeout Tutorial we used an AlarmManager to schedule a task, which fires an intent to close (finish) all the open classes and return a result to the main calling class. Although it did not solve my purpose, which was to securely end the session after a specific time, as in this approach if user changes the time after minimizing the app, it does not work as supposed to. I posted this code to help someone who’s working in similar sort of app, as this code can be utilized in numerous ways. If you like this post please like our facebook page for similar updates.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.


Leave a comment

Your email address will not be published. Required fields are marked *

2 thoughts on “Android Force Close Application : Session Timeout