【问题标题】:Follow up on Password protect launch of android application跟进密码保护Android应用程序的启动
【发布时间】:2012-03-11 09:40:05
【问题描述】:

跟进https://stackoverflow.com/a/3448189,真正显示密码屏幕的最佳方式是什么?

我的第一次尝试是使用 LockActivity 启动子活动:

// 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 在解锁后再次暂停。

所以,我有一些想法:

  1. 使用Fragments 在 MainActivity 中显示主屏幕或锁定屏幕。
  2. Dialog 显示为锁定屏幕(非首选)。
  3. 使用多个if ... else 分支检查是否设置了密码并且 MainActivity 已暂停超过五秒。

举个例子,我想实现与 Dropbox 应用中相同的行为(使用“密码锁定”选项)。

处理这个问题的正确方法是什么?

附:我不确定我是否应该将此作为原始问题的问题发布,从而挖掘出旧线程。我觉得发布一个新问题是一种更简洁的解决方案。

【问题讨论】:

  • 您为什么不喜欢 Dialog 解决方案?

标签: android android-intent android-fragments


【解决方案1】:

既然我是问另一个问题的人,我不妨告诉你我是如何解决它的。我正在使用一个对话框来提示输入密码(我知道你不喜欢它,但它可能会帮助其他人)并确保解除它的唯一方法是输入正确的密码。

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()-method 中,我确保它是不可取消的

protected void onCreate(Bundle savedInstanceState) {
   this.setCancelable(false);
   // ...and some other initializations
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 2017-11-26
    相关资源
    最近更新 更多