Question

I am making my first android app and I want it to be password protected but I am having a small problem with that I knew I should use sharedPreferences but I dont know hot to do that so I need your help with that guys also how could I make my app shows the password activity the first time it run then the login activity in the next time Thank you ...

Était-ce utile?

La solution 2

now u on the right way, u can set a value e.g. (PasswordSet) default this value is false,

now the user type in a password and now the password is set (true) then u can make a method "isPasswordSet" in the start of the application and this proof the value passwordSet. thats all.

the value u can set via the sharedpreferences, here is an example or answer how u realize that: How to use SharedPreferences in Android to store, fetch and edit values

Autres conseils

public class Calc extends Activity {
    public static final String PREFS_NAME = "MyPrefsFile";

    @Override
    protected void onCreate(Bundle state){
        super.onCreate(state);
        //Load your layout

    // Restore preferences
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    String savedPassword = settings.getString("password", /*Put a standard variable here*/
    }
}

Then create a method e.g. checkPassword() which checks whether the given password matches the saved password.

public void checkPassword() {
    if (savedPassword == enteredPassword)
        //Start intent to your login activity
    else
        //Give a toast or something to notify the user that the password was not correct
}

Of course you need a ButtonListener and a variable enteredPassword but I guess you should be able to figure this out.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top