1

I want to quit my application if it stays in background for 5 minutes. Is it possible to do that programmatically?

Jas
  • 3,207
  • 2
  • 15
  • 45

1 Answers1

3

Its possible, run a background service with a timer task which checks the state of the application for a specified time period. This would be generally done using a BaseActivity and overriding onStop() and onWindowFocusChanged() Methods. Find more info here and here

Community
  • 1
  • 1
harshitpthk
  • 4,058
  • 2
  • 24
  • 32
  • great solution!!! I wrote a Service class and set a public variable for storing app state. When onPause() is called, the value is set to 1 and when onResume is called, the value is changed to 0. In my timer task, I check this value and it is working properly, But, I have another problem here. I am planning to use finishAffinity() to clear the app from background. But I am confused about using that in Service class. Could you please help me with that too?? – Jas Jun 22 '16 at 12:17
  • i think you can use finish() or finishAffinity() in your service class, the difference would be that finishAffinity() will close another app if from which your app was opened let suppose say using ACTION_VIEW intent. It depends on your use case if you would want to do that. – harshitpthk Jun 24 '16 at 11:34