我有一個應用程序,我想創建一個登錄活動。 Ive得到 1)登錄(按鈕) 2)用戶名(EditText上) 3)密碼(EditText上)Android應用程序存儲和呼叫用戶名和密碼
我想存儲在設備的內部存儲的用戶名和密碼。如何創建SharedPreferences從中提取並加密的文件?
login_Button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
SharedPreferences userDetails = getSharedPreferences("userdetails", MODE_PRIVATE);
SharedPreferences.Editor edit = userDetails.edit();
edit.clear();
if(etUserID.getText().toString().equals("")){
Toast.makeText(getApplicationContext(),"Please enter a User ID", Toast.LENGTH_LONG).show();
}else if (etPassword.getText().toString().equals("")){
Toast.makeText(getApplicationContext(),"Please enter a password", Toast.LENGTH_LONG).show();
}else{
edit.putString("userID", txtUID.getText().toString().trim());
edit.putString("password", txtPass.getText().toString().trim());
edit.commit();
}
}
});
txtUID和txtPass顯示爲紅色,當我刪除它時,系統表示getText錯誤。
這是我第一次身份驗證。我從android dev頁面找到了一個名爲PasswordAuthentication的東西,但是我一直找不到任何教程或代碼。
SharedPreferences userDetails = getSharedPreferences("userdetails", MODE_PRIVATE);
String UID = userDetails.getString("userID", "");
String pass = userDetails.getString("password", "");
if (userDetails.contains("userID")||userDetails.contains("password")){
startActivity(new Intent(LogOn.this,CrimeMap.class));
}
來源伊夫用於我的研究: http://developer.android.com/reference/java/net/PasswordAuthentication.html how to use getSharedPreferences in android
更新:
好吧,這是我更新的代碼。如果我沒有輸入用戶名或密碼,它會引發我的陳述。但是,當我輸入正確的用戶名和密碼時,程序不會進入下一個活動。我要先讓它運行,然後我繼續對它進行加密。
@Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_log_on);
etUserID = (EditText) findViewById(R.id.etUserID);
etPassword = (EditText) findViewById(R.id.etPassword);
login_Button = (Button) findViewById(R.id.bLogin);
login_Button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
int uid = 1155;
String pass = "pass";
SharedPreferences userDetails = getSharedPreferences(User_File, MODE_PRIVATE);
SharedPreferences.Editor edit = userDetails.edit();
edit.putInt("userID", uid);
edit.putString("password", pass);
edit.commit();
if((etUserID.getText().toString().equals("")) || (etPassword.getText().toString().equals(""))) {
Toast.makeText(getApplicationContext(),"Please enter a User ID", Toast.LENGTH_LONG).show();
}else if (etPassword.getText().toString().equals("")){
Toast.makeText(getApplicationContext(),"Please enter a password", Toast.LENGTH_LONG).show();
}else{
edit.putInt("userID", Integer.parseInt(etUserID.getText().toString()));
edit.putString("password", etPassword.getText().toString());
}
}
});
SharedPreferences userDetails = getSharedPreferences(User_File, MODE_PRIVATE);
if (userDetails.equals(etUserID) && (userDetails.equals(etPassword))){
startActivity(new Intent(LogOn.this,CrimeMap.class));
}
更新:
我終於得到它!謝謝你的幫助!這是我完整的代碼,用於這部分項目。我還沒有加密,但我想我會休息一會兒,稍後再回來。感謝你的幫助!
@Override
public void onClick(View v) {
int uid = 1155;
String pass = "pass";
SharedPreferences userDetails = getSharedPreferences(User_File, MODE_PRIVATE);
SharedPreferences.Editor edit = userDetails.edit();
edit.putInt("userID", uid);
edit.putString("password", pass);
edit.commit();
if((etUserID.getText().toString().equals(""))){
Toast.makeText(getApplicationContext(),"Please enter a User ID", Toast.LENGTH_LONG).show();
}else if (etPassword.getText().toString().equals("")){
Toast.makeText(getApplicationContext(),"Please enter a password", Toast.LENGTH_LONG).show();
}else{
String user_id = etUserID.getText().toString();
int user_id2 = Integer.parseInt(user_id);
String user_password = etPassword.getText().toString();
int userID = userDetails.getInt("userID", 1);
String password = userDetails.getString("password", "no name");
if (userID == user_id2 && password.equals(user_password)){
startActivity(new Intent(LogOn.this,CrimeMap.class));
}
}
您是否直接訪問共享首選項以創建持有用戶名和密碼的文件?現在我正在看「解析」和共享首選項。即使軟件公司使用「解析」?來源:http:// parse。com/ – F0xcr4f7
1.不,上面的代碼已經確實照顧了存儲。 2.我不明白關於解析的問題。是的,它被使用,他們爲每個用例定價,但這與你最初的問題有什麼關係? – Gavriel
sha1不是加密算法。 – njzk2