I am a beginner in android development and i couldn't find out which logic should i apply.
I want to perform a login with File Input/Output in java,
Now i am able to write text to file, text is "username is admin password is admin"
Now when i want to read from this file, i want to apply some login that if the username written in file is "admin" and password written in file is "admin"
then show toast, else error.
I am not able to figure out that how to read keyword "admin" from this text file.
Thanks
Try this,
Create object of sharedPreferences
sharedPreferences = getSharedPreferences("loginDetails", Context.MODE_PRIVATE);
Store login details
private void setLoginDetails(String userName, String password) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("userName",userName);
editor.putString("password",password);
editor.commit();
}
To check login
private boolean isValidUser(String userName, String password) {
if(sharedPreferences.getString("userName",null).equals(userName) && sharedPreferences.getString("password",null).equals(password))
return true;
else
return false;
}
**
Don't save password in sharedPreferences.
** If you really want Its best practice to Encrypt data and save in sharedPreferences. To validate get data from sharedPreferences and Decrypt it.
you can use android shared preference or database if you save a file in android sdcard any one can see this file and can modify it
I think you have not done your research. I have just TYPED "how to read from a file in android" in Google and got the answer in FIRST link. You must do research before asking question here.
By using StringBuilder and BufferedReader you can get all TEXT written in file. Store it in String type variable.
Than, You can EASILY find start, last index of searching word from that String type variable by using indexOf(searching word) and lastIndexOf(searching word).
You can get reference of that methods HERE.
By the way below link has your answer. Just check it out.
Read from text file
the only text that was stored was "username is admin password is admin" So i used string.split method and splited the string and stored it into array and then in the textview i saved the value at specified index of that array
Related
I'm working with an android application, and when my application is open I want to log in. I'm using shared preferences for saving data.
How can I control three cases: if his credentials are null, if that user exists and does not have to register and save his credentials again and also if username and password he entered is correct and is the same with them he used when entered for the first time.
Use this code to retrieve values from sharedPreferences.
SharedPreferences sharedPref ;
sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
int status=sharedPref.getInt("status",0);
if(status==1){
//do something here you want to do
}else if(status==2){
//do something here you want to do
}
Read the documentation here http://developer.android.com/reference/android/content/SharedPreferences.html
Also check this http://www.tutorialspoint.com/android/android_shared_preferences.htm
I'm trying to develop a simple notepad on android. But I don't know how to save my notes(strings) to internal storage(or to an SQL database if it's faster). if I used internal storage would I be able to save a couple of strings and get them back? I'm a beginner to mobile application development and this is my first project. so I'd really appreciate it if you could show me a sample code so I can learn from it. Thanks!
A database is an option, therefore you'll definitively have to read the follow page, that helped me a lot. There is also some sample code in it.
http://www.vogella.com/articles/AndroidSQLite/article.html
In paragraph 9.7 is the full code for adding, editing and deleting records...
An other option is saving the string in an .txt file and save that on the storage. Than this might bring you further:
Write a file in external storage in Android
Good luck!
You can save it in shared preference if it is not too big.
To store:
SharedPreferences sharedPref = getSharedPreferences("SomeName", Context.MODE_PRIVATE);
Editor editor = sharedPref.edit();
editor.putString("String1", value); // value is the string you want to save
editor.commit()
To retrieve:
SharedPreferences sharedPref = getSharedPreferences("SomeName", Context.MODE_PRIVATE);
String retrievedString = sharedPref.getString("String1", defaultValue);
"SomeName" ---- the preference name
"String1" ---- key for the string you want to store/retrieve
defaultValue ---- in case the key is not available, this is the retrieved string
i am new to android development.
i want to create a simple password protected application which asks for a password on launch. the login dialog should ask only for a password or exit. thats it! if password is entered correctly then application should launch or else the user should be asked to enter password again and an exit option should be given.
Answers from with all the details from scratch are most welcome.
Follow these steps :
Make a separate Activity, in its onCreate , pop up an Alert Dialogue.
If the password is correct, then only start your activity using Intent.
else finish() the current activity.
You can save your password either in a database or inside SharedPreferences.
refer internet to use database or SharedPreferences.
eg of SharedPreferences:
SharedPreferences mySharedPreferences;
SharedPreferences.Editor myEditor;
mySharedPreferences = getSharedPreferences("usernamepassworddetails", MODE_PRIVATE);
myEditor = mySharedPreferences.edit();
myEditor.commit();
//In this code you can add the details
myEditor.putInt("username", imageWidth);
myEditor.putInt("password", imageHeight);
myEditor.putInt("position", currentPosition);
myEditor.commit();
//code to retrive data
mySharedPreferences.getInt("username", 0)
mySharedPreferences.getInt("password", 0)
For database related code, refer to this link:
http://www.vogella.de/articles/AndroidSQLite/article.html
Let me know if this of any help.
in my app the first activity is a sign in page. In the edit boxes i am typing the user name and password. Those values are been move to an api and in return i am getting the userid from the server as an xml file.
I am parsing the xml file and storing the value in shared preferrence as follows
SharedPreferences.Editor IdEditor = Id.edit();
IdEditor.putString("useridValue", chap.getid());
IdEditor.commit();
And in the next time when the user opens the app i want to check whether it is already signed i or not. How to check this using the value stored in Shared preference
is your Id class extending SharedPreferences ?
maybe
String userId = Id.getString("useridValue");
If your preference is stored in the default preference then you can
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String userId = prefs.getString("useridValue");
on a side note you shouldn't really use a capital I on the IdEditor variable it should probably be idEditor
Check whether this entry already exist in shared preference, using:
id.containskey("useridvalue")
In android how to search that sharedpreference contains some value or not?
Actually I m making application which takes password and confirm password as fields.when user start app for first time he must enter both password and confirm password. But i want when user restart that app he must ask to enter only password.
For that i store password in sharedPreferences but now how do i know that their is already password exists in sharedPreferences or not?
so that if their is no password in sharedPreferences i can show the activity which contains both password and confirm password to enter AND if there exists password then i wl show activity that contains only password to enter.
If Anyone have idea then please help me.I m tring from many days but still not getting the output.
You can check it by using the contains method on your SharedPreferences instance:
boolean hasPassword = preference.contains("passwordKey");
API Docs:
public abstract boolean contains (String key)
Since: API Level 1
Checks whether the preferences contains a preference.
Parameters
key The name of the preference to check.
Returns
Returns true if the preference exists in the preferences, otherwise false.
for saving data...
SharedPreferences settings = getSharedPreferences("YourKey", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("password", passwordValue);
// Don't forget to commit your edits!!!
editor.commit();
for retrieving...
SharedPreferences settings =this.getSharedPreferences("YourKey", 0);
String userData = settings.getString("password", "0");
if((userData.equals("0"))){
//password has not been saved...
}
else{
//password is already there...
}
fist check if passwort is set:
boolean password_exists = !settings.getString("password","").equals("");
then hide or show the field for the second password
second_passwort_edittext.setVisibility(password_exists ? View.GONE:View.VISIBLE);
After entering the password you can change the behaviour with password_exists (compare the passwords and set them if false, compare with given stored password if true)
I think that you could just validate if you have received something by getting the password value from your shared preferences
if(preferences.getInt("storedPass", 0) != null) {
//Do Stuff
}