2010-11-12 128 views
4

嘿傢伙我需要一個密碼保護我的應用程序的方式,所以當用戶點擊應用程序密碼actitivty首先出現,他們只能訪問應用程序,如果密碼正確。 它的項目即時通訊的一部分,但我卡在這一點。 PLease球員任何hepl將不勝感激。密碼保護應用程序開始

+0

對不起,我忘記了,它的機器人。所以當應用程序啓動時需要登錄屏幕 – Fizzb89 2010-11-12 11:19:59

回答

1

的sharedpreferences檢查假設你有一個按鈕,提交文本編輯字段的上下文:

public class Password extends Activity implements OnClickListener 
{ 
    ... other code 

    public void onCreate(Bundle savedInstanceState) 
    { 
     ...other code 
     Button sumbitButton = (Button) findViewById(R.id.submitbutton); 
     submitButton.setOnClickListener(this); 
    } 

    public void onClick(View v) 
    { 
     EditText passwordEditText = (EditText) findViewById(R.id.passwordedittext); 
     //if people are allowed to set the password on the first run uncomment the following and delete the uncommented section of this function 
     //SharedPreferences prefs = this.getApplicationContext().getSharedPreferences("prefs_file",MODE_PRIVATE); 
     //String password = prefs.getString("password",""); 
     //if(password=="") 
     //{ 
     // SharedPreference.Editor edit = prefs.edit(); 
     // edit.putString("password",passwordEditText.getText().ToString()); 
     // StartMain(); 
     //} 
     //else 
     //{ 
     // if(passwordEditText.getText().ToString()==password) 
     // { 
     //   StartMain(); 
     // } 
     //} 
     if(passwordEditText.getText().ToString()=="your app password") 
     { 
      Intent intent = new Intent(this, your_other_activity.class); 
      startActivity(intent); 
     } 
    } 

    public void StartMain() 
    { 
     Intent intent = new Intent(this, your_other_activity.class); 
     startActivity(intent); 
    } 

這需要在佈局的密碼活動你有一個名爲passwordedittext的edittext和一個名爲submitbutton的按鈕。

而你有你的主要活動(這需要包含在你的清單文件中),你應該用你的主目錄替換你的_other_activity.class。

+0

raybritton我已經嘗試了上述和它不工作。抱歉。因爲是一種痛苦。這是即時通訊使用。 – Fizzb89 2010-11-13 19:50:44

+0

對不起,延遲迴復,哪部分代碼不起作用? – 2010-12-06 12:17:27

+0

它只是不正確更新並繞過登錄。我在首選項中設置了用戶名和密碼。現在,如果我使用允許夸脫在第一次訪問時設置的編碼,則不保存密碼。我想我可能需要一個提交()來保存。 – Fizzb89 2010-12-07 08:25:00

0

從您的密碼活動中獲取文本並將其作爲SharedPreference存儲。 然後每次用戶啓動應用程序執行對已存儲

+4

我建議存儲密碼的散列,而不是密碼本身。 – 2010-11-12 11:32:46

+0

感謝您的快速回復傢伙。即時通訊真的是Android的,所以不知道我會怎麼做。我已經創建了用戶名和密碼的偏好,但不知道我會如何檢查它。 – Fizzb89 2010-11-12 11:35:13

0

錯誤在這行:

if(passwordEditText.getText().ToString()=="your app password") 

應該

if (passwordEditText.getText().ToString().equals("your app password") 

當比較基本數據類型(如int,焦炭,布爾),您可以使用==,=,等等! 比較對象(如字符串,汽車等)時,您需要使用.equals()方法。