简体   繁体   English

如何在Android中密码保护应用程序

[英]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. 我完成了第一部分,因为启动器的意图被触发了,我在该活动的onRestart上得到一个回调。

But by long-pressing home key and launching the does not provide callback to onRestart. 但是通过长按home键并启动不提供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. onResume调用中,在onPause - = 1中设置loginCounter + = 1。

if loginCounter == 0 => show Login Dialog. 如果loginCounter == 0 =>显示登录对话框。

In the Notification Bar you set an Intent to call the Activity , correct? 在通知栏中设置一个Intent来调用Activity ,对吗? Simply add a parameter "isCalledByNotificationBar" as boolean in there. 只需在其中添加一个参数“isCalledByNotificationBar”作为布尔值。

如果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. 也许您应该做的是重新进入应用程序,看看自上次暂停应用程序的任何活动时更新的时间戳以来,它是否少于30(?)秒。 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. 考虑到这个短时间窗口可能会平滑某些转换,您不想注销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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