Description:
Android application which has Login/Logout functionality , They need this type of functionality. In Web / Java Applications Session Time out is given by default but in android there is no default functionality like this.
Here is the example, in which go to next screen and keep screen un-touch for 30 sec. It will show you session time out to alert and when you press "ok" button it will redirect to previous screen.
Here is the full source code:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="40dp"
android:layout_weight="1"
android:src="@drawable/logo" />
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="Go To next screen and hold for 30 sec. It will automatically give you session timeout warning and redirect this page again."
android:layout_marginBottom="10dp"
android:textSize="22dp" />
<Button
android:id="@+id/buttonNextScreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Go To Next Screen" />
</LinearLayout>
MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button btnGoToNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGoToNext = (Button) findViewById(R.id.buttonNextScreen);
btnGoToNext.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
Now, Create another Activity called SecondActivity.java
activity_second.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="22dp"
android:padding="10dp"
android:textAlignment="center"
android:text="Keep untouch this screen for 30 sec. It will show dialog box." />
</RelativeLayout>
SecondActivity.java
import android.os.Bundle;
public class SecondActivity extends MyBaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
Main thing is, In which screen / Activity you want to add session timeout , you need to implement "MyBaseActivity" instead of "Activity".
MyBaseActivity.java
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
public class MyBaseActivity extends Activity {
public static final long DISCONNECT_TIMEOUT = 30000; // 30 sec = 30 * 1000 ms
private Handler disconnectHandler = new Handler() {
public void handleMessage(Message msg) {
}
};
private Runnable disconnectCallback = new Runnable() {
@Override
public void run() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
MyBaseActivity.this);
alertDialog.setCancelable(false);
alertDialog.setTitle("Alert");
alertDialog
.setMessage("Session Timeout, Hit ok to go to previous screen.");
alertDialog.setNegativeButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(MyBaseActivity.this,
MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
dialog.cancel();
}
});
alertDialog.show();
// Perform any required operation on disconnect
}
};
public void resetDisconnectTimer() {
disconnectHandler.removeCallbacks(disconnectCallback);
disconnectHandler.postDelayed(disconnectCallback, DISCONNECT_TIMEOUT);
}
public void stopDisconnectTimer() {
disconnectHandler.removeCallbacks(disconnectCallback);
}
@Override
public void onUserInteraction() {
resetDisconnectTimer();
}
@Override
public void onResume() {
super.onResume();
resetDisconnectTimer();
}
@Override
public void onStop() {
super.onStop();
stopDisconnectTimer();
}
}
AndroidMenifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="session.time.out"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/title_activity_second" >
</activity>
</application>
</manifest>
Download Full Source Code: GitHub
Thank you ..Very clear explanation
ReplyDeleteLove it. amazing
ReplyDeleteamazin info thanks! 😎👍
ReplyDeleteHi I need to increase the milliseconds . For Ex :15 minutes session time out app. Kindly send me an reply
ReplyDeleteHi I'm check your code with increase the timer seconds DISCONNECT_TIMEOUT (Upto 100000) only working.
ReplyDelete