4

I am new to Android App development.I have a small issue.Working on my Android Application if hit on "Home" Button the application goes to background after some time again if go to "Home" and click my app icon it again getting started from my last viewed Activity(screen). Till now its fine,instead of last viewed screen , here i want to Logout automatically and it should show message like "Your Session timed out So Please Login again " or redirect to Login page " Then check the credentials and Allow the user to continue the same application . I am maintaining all the user credentials i.e email,username ,password and user id through out my application by using shared Preference.How to achieve this automatic Logout from application after few hours if it is in background in android or if the application is not used by the user or remains inactive for some times??

Waiting for Your suggestion ??

android learner
  • 1,804
  • 3
  • 15
  • 13
Subhalaxmi
  • 5,687
  • 3
  • 26
  • 42

2 Answers2

0

use TimerTask in android to automatically check the session in asynctask and give particular time in millisecond destory the user Session and show the timeout message in Toast.

Nambi
  • 11,944
  • 3
  • 37
  • 49
  • I think u are not getting me.. If i set particular time if the user is using the application and time completed the application will logout.But my question is that if it is inactive or if the user is not using it than only after few times the user should logout – Subhalaxmi Jan 03 '14 at 12:42
  • or else you have to handle it in webservice – Nambi Jan 03 '14 at 13:09
0

you can try this one its may help you.

1.this is one way:

public boolean onKeyDown(int keyCode, KeyEvent event) {

         if(keyCode == KeyEvent.KEYCODE_HOME)
         {
          Intent intent = new Intent(Intent.ACTION_MAIN);
          intent.addCategory(Intent.CATEGORY_HOME);
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          startActivity(intent);  
          return true;
          }
 return false;
};

2.Another way: you can use onStop() method.It will invoked when the Home button is pressed.In that do your stuff.

Saravanakumar
  • 316
  • 2
  • 10
  • I want to use logout automatically on in active not onStop() – Subhalaxmi Jan 03 '14 at 14:15
  • below code is really works for me please try and let me know. @Override protected void onStop() { // TODO Auto-generated method stub Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); finish(); super.onStop(); } – Saravanakumar Jan 03 '14 at 15:00
  • Please read my question and comment again .. I want to logout my activity when it is ideal not on stop. thanks for your suggestion :) – Subhalaxmi Jan 04 '14 at 07:41
  • Actually If i use your code it will finish the application on activity change every time. I don't want this.. i want if the application is ideal or in inactive state than only it should finish :( – Subhalaxmi Jan 04 '14 at 08:15
  • Sorry..! it works when you have only one activity.can you refer [link](http://stackoverflow.com/questions/4075180/application-idle-time). – Saravanakumar Jan 04 '14 at 10:46
  • Yes but i am looking for full application – Subhalaxmi Jan 04 '14 at 12:20