android - 跟进 Android 应用程序的密码保护启动

标签 android android-intent android-fragments

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

所以,我有一些想法:

  1. 使用 Fragments 在 MainActivity 中显示主屏幕或锁定屏幕。
  2. Dialog 显示为锁定屏幕(不是首选)。
  3. 使用多个 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/

相关文章:

android - 在运行时定义新的 linearLayout 时如何控制 linearLayout 的大小?

java - RecylerView 及其项目的 OnTouchEvent

android-intent - Android - 如何将数据发送回调用 Activity

android - 单击重叠的 UI 元素

android - 后退导航不恢复最后一个 fragment

android - FragmentTransaction 中的 fragment 重复

java - 在 Android 中获取当前星期几的最简单方法是什么?

java - 使用 Kimono Labs 的 Authorize Bearer header 的 GET 请求在 SSL 中的异常

android - 为什么点击按钮后 Activity 没有改变?

Android:检测到另一个应用程序已开始播放音频