简体   繁体   English

Android Lockscreen的好习惯

[英]Android Lockscreen good practice

I am building an android application and one of the requirements is that the application needs to let the user define a passcode to lock the application so, once the passcode was set by the user, the passcode will be required to enter or re-enter the application. 我正在构建一个android应用程序,其中一项要求是该应用程序需要让用户定义密码来锁定该应用程序,因此,一旦用户设置了密码,便需要输入密码才能重新输入密码。应用。

Instead of passcode I am going to use this android library, https://code.google.com/p/android-lockpattern , that provides the same pattern behavior as the home lockscreen, using pattern. 我将使用此Android库而不是密码https://code.google.com/p/android-lockpattern ,该库使用模式提供与家庭锁屏相同的模式行为。

So far, thats ok. 到目前为止,没关系。

If the application is open on the first time, which mean, onCreated() is called, I can check if there is any passcode set and if yes, redirect the user to the screen where he/she needs to enter the pattern. 如果应用程序是第一次打开的,这意味着将调用onCreated(),则我可以检查是否设置了密码,如果是,则将用户重定向到他/她需要输入模式的屏幕。 If pattern valid, ok, user is in, if not, show an error and leave the application. 如果模式有效,确定,则用户在其中,否则显示错误并退出应用程序。 This scenario is ok. 这种情况还可以。

However, if the user hits the home button and re-enter the application, onCreated is not called, so now there wont be any passcode check. 但是,如果用户单击主页按钮并重新输入应用程序,则不会调用onCreated,因此现在将不再进行任何密码检查。 So I was looking at the other activity lifecycle's methods like onPause, onRestart, onResume, etc... all of them are called when the user re-enter the application, and ALSO, when the user navigates back to the Activity by pressing the back button. 因此,我正在查看其他活动生命周期的方法,例如onPause,onRestart,onResume等...,当用户重新进入应用程序时,所有这些方法都将被调用;当用户通过按向后导航到“活动”时,也会调用它们。按钮。

So I am a bit confused/lost on where should this passcode check be besides the onCreated? 所以我对这个密码检查除了onCreated之外应该在哪里感到困惑/迷茫 I dont want to show the passcode screen when the user is navigating thru activities and suddenly returns back to the MainActivity. 当用户浏览活动并突然返回MainActivity时,我不想显示密码屏幕。

How can I make sure the user will be required to enter its passcode either when starting the application or re-entering the application. 如何确保在启动应用程序或重新输入应用程序时要求用户输入密码。

Thank you TL 谢谢TL

Check out the Android Activity Lifecycle diagram. 查看Android活动生命周期图。 I think you're probably looking for onStart , which fires every time a user opens the app, either from the background or as a fresh instance. 我认为您可能正在寻找onStart ,它会在用户每次从后台或作为新实例打开应用程序时触发。

Here's another SO question relating to the same thing: Android onCreate onResume 这是与同一件事有关的另一个SO问题: Android onCreate onResume

I would recommend putting this on a Timer or something similar. 我建议将其放在计时器或类似的东西上。 Whenever the user interacts with your app, the timer (held in an extension to the Application class) starts counting down - from say, 3 minutes. 每当用户与您的应用进行交互时,计时器(保留在Application类的扩展中)就会开始递减计数-从3分钟开始。

Once the counter expires (or, when your activities check in their onResume whether the timer has expired), it starts your top-level login activity again, clearing the back stack as described in this answer . 一旦计数器到期(或者,当您的活动检查其onResume计时器是否已到期)时,该计数器将再次启动您的顶级登录活动, 并按照此答案中所述清除后退堆栈。

This has the benefit of the timer still working while the app is in the background (assuming it's on a separate thread) and the user can still accidentally turn their screen off (for example) and not be forced to log in again. 这样做的好处是,当应用程序处于后台时(假设它在单独的线程中),计时器仍然可以工作,并且用户仍然可以不小心关闭屏幕(例如),而不必被迫重新登录。 My bank's app does something like this. 我银行的应用程序会执行以下操作。

When you switch between Activities, you always call them with an Intent object. 在活动之间进行切换时,始终使用Intent对象调用它们。 The onCreate() method is executed when the Intent is created, but if you unlock your phone, the executed method would be onResume() . 创建Intent时将执行onCreate()方法,但是如果您解锁手机,则执行的方法将是onResume()

You have to control the passcode check in the onResume() method. 您必须在onResume()方法中控制密码检查。 You can't show it every time this method executes, because pressing the back key also executes it. 您不能在每次执行此方法时都显示它,因为按返回键也可以执行它。 Having in mind that you don't want to execute the passcode check only when you're pressing the back key for returning to the previous Activity , maybe you should check, on the onResume() method, if the Intent comes from the Activity or Activities that the user is supposed is coming from. 请记住,您不希望仅在按下返回键以返回到上一个Activity时才执行密码检查,也许您应该在onResume()方法上进行检查,如果Intent来自Activity或用户应该来自的活动。 So, if is not any of these Activities, it will show the passcode check. 因此,如果不是这些活动中的任何一项,它将显示密码检查。

PD: There's also a way to detect if the user comes from unlocking the phone. PD:还有一种方法可以检测用户是否来自解锁手机。 You need to detect if the Intent is an ACTION_USER_PRESENT Intent. 您需要检测该Intent是否为ACTION_USER_PRESENT Intent。 There's more info on this post: https://stackoverflow.com/a/4602341/2367998 该帖子上有更多信息: https : //stackoverflow.com/a/4602341/2367998

I hope it'd bring you help! 希望能为您带来帮助!

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

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