title: how to create a password protected android application
body: <p>I want to create a password-protected android application. The password should be asked when the task is started or when it comes to foreground. </p>
  
  <p>The complex part is that my app can invoke device inbuilt camera application to capture photo/video, so I cannot control that activity.</p>
  
  <p>So the behavior should be that whenever user starts/returns to my app then password should be asked except for the scenario when he is returning from camera that I sent him to in the first place.</p>
  
  <p>I have thought following ideas, but none of them solve my problem completely, the best one I could think of is</p>
  
  <p>1) Set a flag(flagA) while sending the user to camera, then in a service keep checking that the foreground app is our app. When the foreground app is not our app then set another flag(flagB) to indicate that our app has gone to background. When the app in foreground is our app again, then check if flagA is false (we didnt send him to camera) and flagB is true (app is coming from background). Then show the password screen</p>
  
  <p>Problems
  1) The polling interval must be small like 3-4 seconds, so that foreground is detected quickly. This drains the battery.</p>
  
  <p>Thanks</p>