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.
activity_main.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=".MainActivity"
android:background="#FFFFFF" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="Session Timed Out - Please relogin"
android:visibility="invisible" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_marginTop="44dp"
android:src="@drawable/truiton" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Login" />
</RelativeLayout>
MainActivity.java:
package com.Androidtuton.closebroadcast;
import com.truiton.closebroadcast.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.button1);
final Intent i = new Intent(this, SecondActivity.class);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivityForResult(i, 1);
}
});
}
@Override
protected void onResume() {
super.onResume();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
int result = data.getIntExtra("result", 0);
if (result == 1) {
TextView TV1 = (TextView) findViewById(R.id.textView2);
TV1.setVisibility(TextView.VISIBLE);
}
}
if (resultCode == RESULT_CANCELED) {
// code here for cancelled result
}
}
}
}
Second Activity.java
import java.Androidtuton.closebroadcast1;
import com.truiton.closebroadcast.R;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
public class SecondActivity extends Activity {
BroadcastReceiver myReceiver;
AlarmManager alarmManager;
PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_second);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("truiton.ACTION_FINISH");
myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("Second Activity", "Finishing Activity");
/**** Returning Result *****/
Intent returnIntent = new Intent();
returnIntent.putExtra("result", 1);
SecondActivity.this.setResult(RESULT_OK, returnIntent);
/*********/
/**** Cancel Result ****/
/*
* Intent returnIntent = new Intent();
* SecondActivity.this.setResult(RESULT_CANCELED, returnIntent);
*/
/*********/
finish();
}
};
registerReceiver(myReceiver, intentFilter);
}
@Override
protected void onPause() {
super.onPause();
Intent myIntent = new Intent(getBaseContext(),
MyScheduledReceiver.class);
Bundle bundle = new Bundle();
bundle.putInt("val", 8);
myIntent.putExtras(bundle);
pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0,
myIntent, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pendingIntent);
Log.v("Second Activity", "Alarm Scheduled");
}
@Override
protected void onResume() {
super.onResume();
if (alarmManager != null) {
alarmManager.cancel(pendingIntent);
Log.v("Second Activity", "Scheduled Alarm Cancelled");
}
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(myReceiver);
}
}
MyScheduledReceiver.java
package com.truiton.closebroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MyScheduledReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent recievedIntent) {
// TODO Auto-generated method stub
Toast.makeText(context, "Session Timeout", Toast.LENGTH_LONG).show();
Log.v("MyScheduledReceiver", "Intent Fired");
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("truiton.ACTION_FINISH");
context.sendBroadcast(broadcastIntent);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.truiton.closebroadcast"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.truiton.closebroadcast.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="com.truiton.closebroadcast.SecondActivity"
android:label="@string/app_name" >
</activity>
<receiver android:name="MyScheduledReceiver" />
</application>
</manifest>
No comments:
Post a Comment