Password protect the application at startup

Hi guys, I need a way to password protect my application, so when users click on the application, the actitivty password appears first, and they can only access the application if the password is correct. Its part of the project im doing, but I'm stuck on this bit. PLease guys, any hepl would be appreciated.

+4
source share
3 answers

Assuming you have a button that represents the contexts of the TextEdit field:

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); } 

In your layout, a password operation requires an edittext address called passwordedittext and a button called submitbutton.

And you have the main action (which should be included in your manifest file) that you should replace your_other_activity.class.

+1
source

take the text from your password activity and save it as SharedPreference. then every time the user launches the application, a check is performed against the sharedpreferences that you saved.

0
source

The error in this line is:

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

he should be

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

When comparing primitive data types (e.g. int, char, boolean) you can use ==,! =, Etc. When comparing objects (e.g. String, Car, etc.) you need to use the .equals() method.

0
source

All Articles