2012-07-25 105 views
0

我想做一個安全應用程序,要求用戶輸入密碼,如果它是錯誤的密碼對話框(在Theme.Dialog中使用一個活動)將重新啓動,但問題活動未啓動。對於後退按鈕,如果我再按兩次,它將直接關閉對話框。startActivity(意圖)似乎是越野車

package nyp.android.project; 

import java.util.ArrayList; 
import java.util.List; 

import android.app.Activity; 
import android.app.ActivityManager; 
import android.app.ActivityManager.RunningTaskInfo; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.preference.Preference; 
import android.preference.PreferenceManager; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.WindowManager; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class PasswordDialog extends Activity { 

    private static final String TAG = "Logging Activity"; 
    private static boolean isMyServiceRunning; 
    //Context context; 
    Button login; 
    EditText inputPassword; 
    String password; 

    protected void onCreate(Bundle savedInstanceState) 
    { 
     //this.context = context; 
     super.onCreate(savedInstanceState); 
     setContentView (R.layout.password_dialog); 



     final SharedPreferences passwdfile = getSharedPreferences( 
       PhoneFinder.PASSWORD_PREF_KEY, 0); 

     final String correctSHA1 = passwdfile.getString(PhoneFinder.PASSWORD_PREF_KEY, null); 

     login = (Button) findViewById(R.id.btnLogin); 
     inputPassword = (EditText) findViewById(R.id.loginPassword); 


     login.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

       password = inputPassword.getText().toString();    
       final String SHA1hash = PhoneFinder.getSHA1(password); 

       if (correctSHA1.equals(SHA1hash)) { 
        Toast.makeText(PasswordDialog.this, "Correct", Toast.LENGTH_LONG).show(); 
        finish(); 
        stopService(new Intent(getBaseContext(), MyService.class)); 
        Log.v(TAG, "SHA1 Hash:" + SHA1hash); 
        Log.v(TAG, "Correct SHA1:" + correctSHA1); 
       } 
       else { 
        Toast.makeText(PasswordDialog.this, "Wrong", Toast.LENGTH_LONG).show(); 
        Intent Act2Intent = new Intent(getBaseContext(), PasswordDialog.class);    
        startActivity(Act2Intent);   
        finish(); 
        Log.v(TAG, "SHA1 Hash:" + SHA1hash); 
        Log.v(TAG, "Correct SHA1:" + correctSHA1); 


       } 

      } 
     }); 
     } 

    @Override 
    public void onBackPressed() 
    { 
     Intent indent = new Intent(this, PasswordDialog.class); 
     startActivity(indent); 
    } 


} 

回答

1

該問題與後退按鈕:

覆蓋的方法 'onBackPressed()' 的活性。見the docs

@Override 
public void onBackPressed() 
{ 
    Intent indent = new Intent(this, MyActivityToStart.class); 
    startActivity(intent); 
} 

不要調用'super.onBackPressed()',因爲這會關閉活動。

+0

修復了後面的問題!但不是其他人tho :( – dythe 2012-07-25 16:48:28

0

時使用此意圖

Intent Act2Intent = new Intent(PresentActivity.this, PasswordDialog.class);    
startActivity(Act2Intent); 
+0

問題是目前的意圖也是PasswordDialog.class – dythe 2012-07-25 16:37:32

+0

你可以發佈這個類的完整代碼 – 2012-07-25 16:39:28

+0

PasswordDialog是目前的類...我已經說過即時開始這個活動作爲一個主題。對話框顯示爲對話窗口,而我想要用戶輸入密碼,如果它不正確,那麼它會重新啓動此意圖 – dythe 2012-07-25 16:48:09

0

找到了解決這個問題我不得不調用finish(); before startActivity()

+0

如果這回答這個問題,然後在這裏添加複選標記。 – RaphMclee 2012-07-26 06:07:07

+0

直到明天才能接受我自己的問題。 – dythe 2012-07-26 06:11:52