简体   繁体   中英

How to password protect an application in Android

I want the user to enter a password everytime he tries to enter the application. That is, the user must enter the password everytime the app comes to foreground from background, be it by pressing the launcher icon or long-pressing the home key

I sort-of achieved the first part because the launcher intent is fired and i get a callback in onRestart of that activity.

But by long-pressing home key and launching the does not provide callback to onRestart.

Also what if the user launches the app by pressing the notification from, the notification bar. How do I distinguish whether the app was originally in background or fore-ground before the user clicked the notification

In your onResume call, set a loginCounter += 1, in your onPause -= 1.

if loginCounter == 0 => show Login Dialog.

In the Notification Bar you set an Intent to call the Activity , correct? Simply add a parameter "isCalledByNotificationBar" as boolean in there.

如果onResume不起作用, onWindowFocusChanged(boolean)怎么样?

Perhaps what you should do is on re-entry into the application see if it's been less than 30 (?) seconds since a timestamp that you update when any activity of your application was last paused. If it's been less, don't ask for a password. Thinking being that this short time window might smooth over some transitions you don't want to log out on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM