2015-12-16 30 views
-1

我有一個Activity,我想在其中輸入PIN碼。在這個Activity用戶可以去單獨的Activity,他可以在其中設置PIN。如果未設置PIN,則應使用默認PIN碼。Android使用Shared Preference作爲密碼

這是第一個Activity的代碼:

public class activity_identification extends AppCompatActivity { 

    SharedPreferences pins_pref = null; 
    EditText pin_entry = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_identification); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 
     pin_eingabe = (EditText) findViewById(R.id.et_pin_entry); 

     pins_pref = getApplicationContext().getSharedPreferences("UserPin", Context.MODE_PRIVATE); 
     SharedPreferences.Editor edit = pins_pref.edit(); 
     edit.putString("default_pin", "0000"); 
     edit.commit(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_pin_identifikation, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 

     if (id == R.id.action_change_pin) { 
      startActivity(new Intent(this, pin_settings.class)); 
      return true; 
     } 

     else if (id == R.id.home) { 
      this.finish(); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 


    public void onClickPinSummit(View button){ 

     if(pin_entry.getText().toString().equals(pins_pref.getString("user_pin", null).toString())) { 
      Toast.makeText(this, "Authorization Right", Toast.LENGTH_SHORT).show(); 
     } 
     else { 
      Toast.makeText(this, "Authorization Wrong", Toast.LENGTH_SHORT).show(); 
     } 


    } 
} 

這是第二Activity

public class pin_settings extends AppCompatActivity { 

    EditText et_old_pin = null; 
    EditText et_new_pin = null; 
    EditText et_new_pin_repeat = null; 
    Button btn_pin_save = null; 
    TextView tv_sharedPIN = (TextView) null; 

    SharedPreferences pins_prefs = null; 
    private String default_pin = null; 
    private String user_pin = null; 
    private String old_pin = null; 
    private String new_pin = null; 
    private String new_pin_repeat = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_pin_einstellungen); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     et_old_pin = (EditText) findViewById(R.id.et_old_pin); 
     et_new_pin = (EditText) findViewById(R.id.et_new_pin); 
     et_new_pin_repeat = (EditText) findViewById(R.id.et_new_pin_repeat); 
     btn_pin_save = (Button) findViewById(R.id.btn_pin_save); 
     tv_sharedPIN = (TextView) findViewById(R.id.sharedPIN); 

     pins_prefs = getApplicationContext().getSharedPreferences("UserPin", Context.MODE_PRIVATE); 
     general_pin = pins_prefs.getString("default_pin", null); 

     tv_sharedPIN.setText(pins_prefs.getString("general_pin", null)); 
    } 

    public void onClickNewPinSave(View button){ 
     old_pin = et_old_pin.getText().toString(); 
     new_pin = et_new_pin.getText().toString(); 
     new_pin_repeat = et_new_pin_repeat.getText().toString(); 

     if(old_pin.equals(pins_prefs.getString("default_pin",null).toString())){ 
      if (new_pin.equals(new_pin_repeat)){ 
       SharedPreferences.Editor edit = pins_prefs.edit(); 
       edit.putString("user_pin", user_pin); 
       edit.commit(); 
       tv_sharedPIN.setText(pins_prefs.getString("user_pin", null)); 
      } else { 
       Toast.makeText(this, "New Pins are not the same", Toast.LENGTH_LONG).show(); 
       } 
     } else { 
      Toast.makeText(this, "Old Pin is wrong.", Toast.LENGTH_LONG).show(); 
      } 
    } 
} 

現在的問題是,應用程序不保存這是在設定的PIN第二個Activity。只要應用程序處於打開狀態,它就會工作,但只要重新啓動,PIN碼就會恢復爲默認PIN碼。我認爲這是因爲我在第一個ActivityonCreate()方法中設置了默認PIN碼,但我無法弄清楚如何解決這個問題。

+0

「Glabal」是什麼意思分享了prefrences? – JIGAR

+0

您可以從任何獲取上下文的地方訪問'SharedPreferences'。 –

+0

你是什麼意思的上下文?我認爲我不太瞭解共享偏好功能原理.. –

回答

1

編輯:由於Xaver Kapeller指出我以前的解決方案不會工作。雖然他的一個更簡單,我會修正我的只是因爲

設置default_pin0000如果getString(default_pin, "-1")返回"-1"

OLD:嘗試僅僅設置default_pin0000如果default_pin null,它只會在第一個推出的活動

+0

你似乎缺少傳遞給'getString()'的默認值的點... ... –

2

當然PIN將永遠被重置爲默認發生,因爲你設置的每個啓動時間。所有你需要做的修復是刪除該行,而是使用正確傳遞到getString()的默認值。

  1. 根本不需要設置默認PIN碼!從onCreate()中刪除全部
  2. getString()接受默認值作爲第二個參數。如果未設置任何值,則返回此默認值。讓我重複一遍:如果未設置任何值,則默認值爲return。

    因此,您不需要在onCreate()中設置默認PIN碼,只需將默認PIN碼傳遞到getString(),如果沒有設置PIN碼,getString()將返回默認PIN碼。所以,你正在尋找的是這樣的:

    String pin = preferences.getString("default_pin", "0000"); 
    

在一個側面說明:請堅持常見的Java命名約定。也不要硬編碼那麼多String s。改用常量。

+0

那麼我需要做哪些修改?在第一個活動中,我必須刪除'edit.putString(「general_pin」,「0000」); '?而在第二個活動?我在哪裏必須初始化共享偏好?當我在第一個活動中詢問「user_pin」時,應用程序崩潰... –

+0

@TobiF。我已經告訴過你需要改變什麼。一切都保持不變,但是你可以去掉'putString()'調用,它在'onCreate()'中設置默認的引腳,然後像以前一樣使用它。但不是調用'getString(「default_pin」,null)'使用'getString(「default_pin」,「0000」)''。 –

相關問題