2012-03-11 100 views
3

跟进https://stackoverflow.com/a/3448189,实际上显示密码屏幕的最佳方法是什么?后续密码保护启动android应用程序

我的第一次尝试是启动子活动与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); 
    } 
} 

然而,这会导致在MainActivity再次得到解锁如果显示超过5秒的LockActivity后暂停。

所以,我有一些事情记:

  1. 使用Fragments显示主屏幕或锁定屏幕MainActivity内。
  2. 显示Dialog锁定屏幕(不是首选)。
  3. 使用多个if ... else分支来检查是否已设置密码 MainActivity的暂停时间超过5秒。

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

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

P.S.我不确定我是否应该将此作为问题发布到原始问题上,从而挖掘出旧的主题。我觉得发布一个新问题是一个更清洁的解决方案。

+0

为什么你不喜欢一个对话的解决方案? – 2012-03-29 06:52:35

回答

3

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

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 
}