메모장 앱을 쓰고 있는데, 내가 만들 때까지 create a password
화면이 튀어 나오고 그때부터 log in
화면이 나타납니다.안드로이드는 내 파일을 기억하지 못합니다.
File myFile = new File(getFilesDir() + "pass.txt");
if (!myFile.exists()) // if "pass.txt" DOESN'T exist, make them create a password
{
try {
// this writes the password to the "pass.txt" file
// which is the one that is checked to exist.
// after it is written to, it should always exist.
FileOutputStream fos = openFileOutput("pass.txt", Context.MODE_PRIVATE);
fos.write(pass.getBytes());
// this writes the security question to a different file.
fos = openFileOutput("securityQ.txt", Context.MODE_PRIVATE);
fos.write(secQ.getBytes());
// this writes the security answer to a different file.
fos = openFileOutput("securityAnswer.txt", Context.MODE_PRIVATE);
fos.write(secAns.getBytes());
fos.close();
} catch(Exception e) {}
^그 한 방법이다 : 여기
일부 샘플 코드이다. 그런 다음, 또 다른 내가 이렇게 :try { // input the right password to the String data
char[] inputBuffer = new char[1024];
fIn = openFileInput("pass.txt");
isr = new InputStreamReader(fIn);
isr.read(inputBuffer);
data = new String(inputBuffer);
isr.close();
fIn.close();
}catch(IOException e){}
if (password.getText().toString().equals(data)) // if password is right, log in.
{
loggedin();
}
else // if the password entered is wrong, display the right one.
{
TextView scr = (TextView)findViewById(R.id.display);
scr.setText("." + data + "."+'\n'+"." + password.getText().toString() + ".");
}
문제는 사용자가 암호를 올바르게 입력해도 로그인 할 수 있다는 것입니다 디스플레이는 것을 증명한다.
또 다른 문제는 앱을 다시 실행할 때마다 작성 화면으로 이동한다는 것입니다. 즉, 파일이 존재하지 않는 것을 인식한다는 의미입니다 (방금 썼음에도 불구하고).
나는이 전체 프로젝트 파일을 처리했으며 입력 된 텍스트를 추적하여 단추를 누르면 다시 파일을 읽습니다. 닫을지라도 입력 한 내용을 추적합니다. 그러나 어떤 이유로 암호가 작동하지 않습니다. 받는
SOLUTION : 여기
어떻게되는지의 이미지이다 (제.k.
는 EditText
에서 String
입력 된 사용자 "pass.txt"
파일 및 제 .k.
로부터 판독 된 데이터이다) 문제 로그인 :
String values look the same but don't ".equals()" each other
,536,913,632 10암호 사용자 입력에서 .trim()
메서드를 사용하기 만하면됩니다.
암호 및 사용자 이름을 저장하려면 SharedPreferences를 사용해야한다고 생각합니다. http://developer.android.com/reference/android/content/SharedPreferences.html – Tobiel
@ 토비 엘 : 나는 그가 환경 설정에서 사용자 이름을 저장해야한다고 생각한다. 패스워드와 같은 중요한 정보를 저장하는 것은 기본 설정을 읽을 수 없으므로 가능하지 않다. –