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.
Related
I can not understand a thing.
I am creating an android app that is connected to the DB.
When the user registers after login, once he is logged in, if he closes the application and reopens it, he makes it all over again.
I want that when the user reopens the app if he has not done the Logout he must be in the activity of the Login.
Can you advise me what to use?
The SharedPreference?
Apps like Facebook, Instagram and other things use?
PS. They are many users.
Thank you very much for helping.
You can use SharedPrederence for session management of Login/Signup :
When User login Hit API or Internal DataBase with validation & get all user details & store it in SharedPreferences.
Next time when User again open app first it will check the status of sharedPreference if login get Data otherwise Login screen will come.
When User Logout Crear all Login Data.
Initialization :
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
Storing Data :
editor.putBoolean("login_status", true); // Storing boolean - true/false
editor.putString("name", "string value"); // Storing string
editor.putInt("user_id", "int value"); // Storing integer
editor.commit(); // commit changes
Retrieving Data :
pref.getString("name", null); // getting String
pref.getInt("user_id", 0); // getting Integer
Reference : https://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/
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
Making a app that asks user to create a profile, wondering where I should get started in having the app remember this data user inputs? Any tutorials or suggestions would be appreciated.
Thanks,
Grant
I suggest you use SharedPreferences, unless you have a lot of information to store.
After the user successfully logged in, store his information in your Preferences.
For example, to store the username :
private SharedPreferences mPreferences;
mPreferences = getSharedPreferences("User", MODE_PRIVATE);
SharedPreferences.Editor editor = mPreferences.edit();
editor.putString("username", your_user_name);
editor.commit();
Each time the user accesses the login activity, you can check if the username is already stored in the preferences :
if (mPreferences.contains("username")) {
// start Main activity
} else {
// ask him to enter his credentials
}
When the user logs out, don't forget to delete the username key from your preferences :
SharedPreferences.Editor editor = mPreferences.edit();
editor.clear(); // This will delete all your preferences, check how to delete just one
editor.commit();
Insert the data to a SQLite database or use a plain file. The former is recommended for big apps.
Checked out SharedPreferences. Some examples here: SharedPreferences Tutorial
If you only have one user at once and just need to store simple user data like user name, email, id, you can store string/int/... format data in it. Or if you have server for storing user data, you can store credentials in SharedPreference and use the credentials to get data from your server.
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
}
I have 3 webviews in my search engine app.When the user enters his query, by default he gets Google results, followed by yahoo and Ask buttons at the bottom, on clicking either of them, he gets results for that query from those sites. Now I want to give the user the privilege to change the default result site. I have created 3 radiobuttons. Upon confirmation, say he chooses Yahoo, how can i set it as Yahoo till the next time he changes it to some other site,
Accessing data from SharedPreferences:
SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
String webViewChoice = sharedPref.getString("userChoice","null");
if no choice was saved (in the case when the application is running for the first time), you'll get "null" in webViewChoice.
use this condition as you wish
Saving data in SharedPreferences:
SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putString("userChoice",usersChoice);
prefEditor.commit();
I hope it helps.
Save the user's preference as default-engine=google by default in a shared preferences file.
On app loading, read the file and set the default engine during the app runtime. When user chooses a different engine as default, then update the preferences file.
Hope this helps.