I am writing an app to save password with a log in interface. The user can change the log in password. I use the following code to save the password, so that the password will not reset when the app relaunch.
#Override
public void onSaveInstanceState(Bundle savedInstanceState){
savedInstanceState.putString("pwd", currentPwd);
super.onSaveInstanceState(savedInstanceState);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState){
currentPwd = savedInstanceState.getString("pwd");
}
But I found that it can only save the password for a while. When I wait it for a long time, about 1 hours, without reboot the mobile, it will reset my password.
How to save the password so that it will not reset?
you have to save the data using sharedpreferences:
SharedPreferences prefs =
getSharedPreferences("myprefname",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("pwd", "thepassword");
editor.commit();
then you can retreive it:
SharedPreferences prefs =
getSharedPreferences("myprefname",Context.MODE_PRIVATE);
String password=prefs.getString("pwd",null);
You can save that type of information for a long time using sharedPreference.
public class PreferenceData
{
static final String PREF_USER_PASSWORD = "user_password";
public static SharedPreferences getSharedPreferences(Context ctx)
{
return PreferenceManager.getDefaultSharedPreferences(ctx);
}
public static void setUserPassword(Context ctx, String userPassword)
{
Editor editor = getSharedPreferences(ctx).edit();
editor.putString(PREF_USER_PASSWORD, userPassword);
editor.commit();
}
public static String getUserPassword(Context ctx)
{
return getSharedPreferences(ctx).getString(PREF_USER_PASSWORD, "");
}
}
I have the exact same problem as you or did have. I simply used Shared_Prefs. It saves the login data in an XML file and you read and write to it.
Here's the Android writeup: http://developer.android.com/guide/topics/data/data-storage.html
To write:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("key1", username);
editor.putString("key2", password);
editor.commit();
To Read:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String username = settings.getString("key1", null);
String password = settings.getString("key2", null);
Related
Hy, I'm writing an application for a project. I'm trying to pass data between two activities. I tried to use the SharedPreference but it's doesn't work. The output send me always " ".
I post the two function below.
function for send data:
public void SaveUser(FirebaseUser user){
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(TEXT, user.getDisplayName());
}
function for get data:
public String ReturnCreatorName(){
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
String name = sharedPreferences.getString(TEXT, "");
return name;
}
you forgot editor.commit();
public void SaveUser(FirebaseUser user){
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(TEXT, user.getDisplayName());
editor.commit();
}
I use SharedPreferences in my app to save small Integer data . But I get ClassCastException in this line .
int number = mySharedPref.getInt("numberOne",0);
Here is my code .
To store data .
mySharedPref=getSharedPreferences("MyPref",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mySharedPref.edit();
editor.putInt("numberOne",myInteger);
To get data
mySharedPref = getSharedPreferences("MyPref",Context.MODE_PRIVATE);
int number= mySharedPref.getInt("numberOne",0);
display(number);
I think the problem is that you have not committed the data stored in SharedPreferences.
So you should do a
editor.commit();
while storing the data.
Try this
To Store data
public static void setInteger(Context context, String key, Integer Value) {
sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
SharedPreferences.Editor sEdit = sharedPreferences.edit();
sEdit.putInt(key, Value);
sEdit.commit();
}
To get data
public static Integer getInteger(Context context, String key) {
sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
Integer value = sharedPreferences.getInt(key, 0);
return value;
}
You have to call commit() method to save data changes in SharedPreferences.
Here is the get data method
private void showData() {
SharedPreferences sharedPreferences = getSharedPreferences("MyFile", Context.MODE_PRIVATE);
int number = sharedPreferences.getInt("numberOne", 0);
display(number);
}
Here is get savemethod
private void saveData(int value) {
SharedPreferences sharedPreferences = getSharedPreferences("MyFile", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("numberOne", value);
editor.commit();
}
Help please, I'm a bit lost, new to all this and trying to learn.
I want to store 3 names and phone numbers in SharedPreferences and display them in individual textviews so a user can edit the name and phone number and also press the number to call the number.
What I have so far is:
public void setWardenName(Context context, String key, String value) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(getString(R.string.), Name);
editor.apply();
}
public void getWardenName(Context context, String prefKey) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String name = preferences.getString("Name", "");
if(!name.equalsIgnoreCase(""))
{
name = name;
}
}
And on the activity screen I have the first texview called:
android:id="#+id/textView2" which was going to be the first name.
So I'm lost, any help is appreciated.
I will set the Warden as your key name and it depends on you if you want to change that. And, I will assume that it is in the same class since you are using 'this' in
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
So here it is..
private void setWarden(String value) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = sharedPref.edit();
editor.putString("Warden", value);
editor.commit();
}
private String getWarden() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String name = sharedPref.getString("Warden", "");
if(!name.equalsIgnoreCase(""))
{
return name;
}
return "";
}
And in your textview you can get it like:
TextView tv = (TextView) findViewById(R.id.textView2);
tv.setText(getWarden());
I have try to set and store the password for my apps, but it is not working at all. The password should be setted first time then return the home page, then when the user open it again the password should be stored but somehow it didn't store it.
PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences passfile = getSharedPreferences("ans",0);
String pass = passfile.getString("ans", null);
check.setOnClickListener(new OnClickListener () {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String answer1 = answer.getText().toString();
//Check that user typed in an answer
if(answer1.length()<8){
Toast.makeText(CheckPwActivity.this, "Answer must be 8 characters long", Toast.LENGTH_SHORT).show();
answer.setText("");
answer.requestFocus();
return;
}
answer.getEditableText().toString();
//check if the answer is valid
if (answer1.equals("ans")) {
Intent intent2 = new Intent(CheckPwActivity.this,MainActivity.class);
startActivity(intent2);
}else{
return;
}
}});
}
public void setPassword(String key, String value) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
Editor preferenceEditor = context.getSharedPreferences("password", 8).edit();
preferenceEditor.putString(key, value);
preferenceEditor.commit();
}
public static String getPassword(String filename) {
return context.getSharedPreferences("password", 2).getString(filename,"");
}
you are using shared preferences wrong, in your set password you get shared preferences but then you get it again but in a different context
this is all you need to use shared preferences
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
then when you want to set it you use the editor you just got
editor.putString(key,pass).commit;
then to get it from shared preferences you would just do
preferences.getString(key,defaultString);
I am using the code for remember my username and password. i have tried a lot of hours for the code for remember my username name and password but i cant success in it.
Here is my code which i have download from internet but it didn't work for me.
I have declared this variable in the extends activity.
public static final String PREFS_NAME = "MyPrefsFile";
public static final String PREFS_USER = "prefsUsername";
public static final String PREFS_PASS = "prefsPassword";
after that I have also declare different variable like below.
public String PREFS_USERS;
public String PREFS_PASS;
String username;
String upass;
and in the click listener i have given the following code.
SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
username = etUsername.getText().toString();
upass = etPassword.getText().toString();
getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
.edit()
.putString(PREFS_USERS, username)
.putString(PREFS_PASS, upass)
.commit();
and at the time at retry i have return the following code in the oncreate activity to retry my user name and password.
SharedPreferences pref = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
username = pref.getString(PREFS_USERS, "");
upass = pref.getString(PREFS_PASS, "");
But when I run the application i cant get the username and password.First time when i load the application i checked the remember me check box after log in and log out back from the application and close the application and when come back to it.it was not saved for me.
The problem is in declaring the variables
you have declared
public static final String PREFS_NAME = "MyPrefsFile";
public static final String PREFS_USER = "prefsUsername";
public static final String PREFS_PASS = "prefsPassword";
public String PREFS_USERS;
public String PREFS_PASS; //assigned null here
.putString(PREFS_PASS, upass) //here value of PREFS_PASS is null
Use the following code
better do like the folowing
public String PREFS_USERNAME= "prefsUsername";
public String PREFS_PASSWORD="prefsPassword";
Store the data as using following code
SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
passwordInString = password.getText().toString();
userNameInString = username.getText().toString();
getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
.edit()
.putString(PREFS_USERNAME, userNameInString)
.putString(PREFS_PASSWORD, passwordInString)
.commit();
retrieve the data like the following
SharedPreferences pref = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
String usernameName = pref.getString(PREFS_USERNAME, "");
String upassWord = pref.getString(PREFS_PASSWORD, "");
Thanks
Deepak
It should look something like this (obviously you can use global vars so you dont have to specify them in several places):
String PREFS = "MyPrefs";
SharedPreferences mPrefs;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mPrefs = getSharedPreferences(PREFS, 0);
//check if the remember option has been check before
boolean rememberMe = mPrefs.getBoolean("rememberMe", false);
if(rememberMe == true){
//get previously stored login details
String login = mPrefs.getString("login", null);
String upass = mPrefs.getString("password", null);
if(login != null && pass != null){
//fill input boxes with stored login and pass
EditText loginEbx = (EditText)findViewById(R.id.login_box);
EditText passEbx = (EditText)findViewById(R.id.pass_box);
loginEbx.setText(login);
passEbx.setText(upass);
//set the check box to 'checked'
CheckBox rememberMeCbx = (CheckBox)findViewById(R.id.remeber_cbx);
rememberMeCbx.setChecked(true);
}
}
}
private void saveLoginDetails(){
//fill input boxes with stored login and pass
EditText loginEbx = (EditText)findViewById(R.id.login_box);
EditText passEbx = (EditText)findViewById(R.id.pass_box);
String login = loginEbx.getText().toString();
String upass = passEbx.getText().toString();
Editor e = mPrefs.edit();
e.putBoolean("rememberMe", true);
e.putString("login", login);
e.putString("password", upass);
e.commit();
}
private void removeLoginDetails(){
Editor e = mPrefs.edit();
e.putBoolean("rememberMe", false);
e.remove("login");
e.remove("password");
e.commit();
}
You would probably call the saveLoginDetails() & removeLoginDetails() methods in the OnClick listener:
CheckBox rememberMeCbx = (CheckBox)findViewById(R.id.remeber_cbx);
boolean isChecked = rememberMeCbx.isChecked();
if(isChecked){
saveLoginDetails();
}else{
removeLoginDetails();
}
I hope you can find it useful
U can do this.. create a boolean variable with default value "false" and set a boolean value to true when user choses for rememberMe and put an if condition before validating user password and other stuff.. and use the boolean value in this condition..
SharedPreferences myPrefs;
onCreate()
{
myPrefs=this.getSharedPreferences("PREF_NAME",Context.MODE_PRIVATE);
if(myPrefs.getBoolean("firstTime", false))
{
startActivity(new Intent(this,SeeRecordsActivity.class));
finish();
}
}
on signIn button:
boolean c=chkbox.isChecked();
if(c==true)
{
SharedPreferences.Editor editor=myPrefs.edit();
editor.putBoolean("firstTime", true);
editor.commit();
}