跟进 https://stackoverflow.com/a/3448189 ,实际显示密码屏幕的最佳方式是什么?
我的第一次尝试是使用 LockActivity 启动 SubActivity:
// MainActivity.java
public void onResume() {
super.onResume();
ApplicationState state = ((ApplicationState) getApplication());
if ((new Date().getTime() - state.mLastPause) > 5000) {
// Prompt for password if more than 5 seconds since last pause
Intent intent = new Intent(this, LockActivity.class);
startActivityForResult(intent, UNLOCKED);
}
}
但是,如果 LockActivity 显示超过 5 秒,这会导致 MainActivity 在解锁后再次暂停。
所以,我有一些想法:
- 使用
Fragments
在 MainActivity 中显示主屏幕或锁定屏幕。 - 将
Dialog
显示为锁定屏幕(不是首选)。 - 使用多个
if ... else
分支来检查密码是否已设置并且 MainActivity 是否已暂停超过五秒。
举个例子,我想实现与 Dropbox 应用程序相同的行为(使用“密码锁定”选项)。
处理这个问题的正确方法是什么?
附言我不确定我是否应该将此作为问题发布到原始问题,从而挖掘出旧线程。我觉得发布一个新问题是一个更简洁的解决方案。
最佳答案
既然是我问的另一个问题,那我不妨告诉你我是如何解决的。我正在使用对话框提示输入密码(我知道你不喜欢,但它可能对其他人有帮助)并确保关闭它的唯一方法是输入正确的密码。
MyApplication app = ((MyApplication)getApplication());
if (new Date().getTime() - app.mLastPause > 5000) {
// If more than 5 seconds since last pause, check if password is set and prompt if necessary
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
String password = pref.getString("password", "");
if (password.length() > 0) {
// Prompt for password
MyPasswordDialog dlg = new MyPasswordDialog(this, password);
dlg.setOwnerActivity(this);
dlg.show();
}
}
并且在 MyPasswordDialog 的 OnCreate()
方法中,我确保它不可取消
protected void onCreate(Bundle savedInstanceState) {
this.setCancelable(false);
// ...and some other initializations
}
关于android - 跟进 Android 应用程序的密码保护启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9653866/