2012-03-11 8 views
3

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); 
    } 
} 

しかし、これはMainActivityがLockActivityが5秒以上表示されている場合にロック解除取得した後、再び一時停止されます。だから、

、私は心の中でいくつかのものがあります。MainActivityの内側にメイン画面やロック画面を表示するために使用Fragments

  1. を。
  2. ロック画面としてDialogを表示します(推奨しません)。
  3. 複数のif ... elseブランチを使用して、パスワードが設定されているかどうかを確認するおよび MainActivityが5秒より長く一時停止されています。

例を挙げれば、私はDropboxアプリケーション(「パスコードロック」オプションを使用)と同じ動作を達成したいと思います。

これを処理するための正しい方法は何ですか?

P.S.私はこれを元の質問への質問として投稿して、古いスレッドを掘り起こすべきかどうかはわかりません。新しい質問を投稿するような気がするのは、よりクリーンな解決策です。

+0

キャンセルできないことを確認してください? –

答えて

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() -methodに私はそれがなぜあなたはダイアログのソリューションを好まない

protected void onCreate(Bundle savedInstanceState) { 
    this.setCancelable(false); 
    // ...and some other initializations 
} 
関連する問題