Added read password from file.

This commit is contained in:
Marcin Wozniak 2019-10-17 13:40:41 +02:00
parent f36506d92e
commit 7ac96f9de4

View File

@ -12,15 +12,40 @@ import android.widget.EditText;
import android.widget.TextView; import android.widget.TextView;
import java.io.File; import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
TextView response; TextView response;
Button button; Button button;
Button restartpassword; Button restartpassword;
private EditText textPassword; private EditText textPassword;
private View viewPassword; private View viewPassword;
private static final String filename = "passwd.txt";
public String readFile(String filename) throws IOException {
String content = null;
File file = new File(filename); // For example, foo.txt
FileReader reader = null;
try {
reader = new FileReader(file);
char[] chars = new char[(int) file.length()];
reader.read(chars);
content = new String(chars);
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(reader != null){
reader.close();
}
}
return content;
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -42,14 +67,19 @@ public class MainActivity extends AppCompatActivity {
}); });
button.setOnClickListener(new OnClickListener() { button.setOnClickListener(new OnClickListener() {
private final String filename = getFilesDir() + "/passwd.txt";
@Override @Override
public void onClick(View view) { public void onClick(View view) {
response.setText(textPassword.getText()); response.setText(textPassword.getText());
if (textPassword.getText().toString().equals("123")) { try {
Intent myIntent2 = new Intent(MainActivity.this, if (textPassword.getText().toString().equals(readFile(filename).toString())) {
Noto.class); Intent myIntent2 = new Intent(MainActivity.this,
startActivity(myIntent2); Noto.class);
finish(); startActivity(myIntent2);
finish();
}
} catch (IOException e) {
e.printStackTrace();
} }
} }
}); });