How to deal with multi accout login in android? - android

I have several apps which use the same accout.
The question is when i had more than one apps installed in my phone,how can i login without type username and password if one of the app had logged in.

See below reference code which is having app1 & app2, so using shared preferences suppose you can store username & password in app1 & you can using it in another app2. Here I'm shwing how to access username, you can expand it as per your requirement.
Okay! using this code in Application 1 ( with package name is "com.sharedpref1" ) to store data with Shared Preferences.
SharedPreferences prefs = getSharedPreferences("demopref",
Context.MODE_WORLD_READABLE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("username", strShareValue);
editor.commit();
And using this code in Application 2 to get data from Shared Preferences in Application 1. We can get it because we use MODE_WORLD_READABLE in application 1:
try {
con = createPackageContext("com.sharedpref1", 0);
SharedPreferences pref = con.getSharedPreferences(
"demopref", Context.MODE_PRIVATE);
String data = pref.getString("username", "No Value");
displaySharedValue.setText(data);
} catch (NameNotFoundException e) {
Log.e("Not data shared", e.toString());
}

I think, you could use Database for storing username and password, and share the database among your multiple apps, settings some field like is_logged_in with boolean value 1 when user is loggedin. On app launch, access the database, check for the field is_logged_in and display appropriate logged in view.

Related

How secure Shared Preferences data in my application from Security Auditor

How to secure SharedPreferences data in my application?.
I use 2 step for this but in Security Audit hacker are able to hack my data.
1 Step-
SharedPreferences sharedPreferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("key", value);
editor.commit();
2 Step- Use SecureSharePreferences
SecurePreferences securePrefs = new SecurePreferences(context, "key", "my_user_prefs.xml");
SharedPreferences.Editor editor =securePrefs.edit();
editor.putString(key, value);
editor.commit();
Any other method to handle this.
After read some Answer I update my code with encrypt data but problem still exist.Security auditor still getting application sharedpreference.file from app memory.
SharedPreferences sharedPreferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
String keyEncript = EncriptionDecriptionUtils.encriptionOfData(key).toString().trim().replaceAll("\r\n", "");
String value = sharedPreferences.getString(keyEncript, "").trim().replaceAll("\r\n", "");
String valuedecript = EncriptionDecriptionUtils.decriptionOfData(value).toString().trim().replaceAll("\r\n", "");
return valuedecript;
You can encrypt & decrypt the shared preferences data using AES algorithm.If you open shared preferences explicitly you will get encrypted information only.For your reference look into this Securing SharedPreferences Data using AES algorithm
On a rooted phone, it can access the shared preferences for your app. Also, on any phone the user can delete all the data that it's stored in shared preferences by clearing the cache in the application manager. A safe way to store data would be to encrypted it with AES and save it in a text file in the root folder of your app.

Android One-time Login screen using SharedPreferences

I am Wondering about One-time screens... I know, I should use something like SharedPreferences or stuff like that.
If someone has a simple solution for one-time login screen. And a little example.
My login contains: weight, name , height, age and gender (spinner)
You can take a look at Android User info and Sign in :
https://developer.android.com/training/sign-in/index.html
Or you can use login with Facebook API.
Otherwise, I would use Shared prefs.
Create a shared prefs file
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
This will create a private file for the current activity. You can use MODE_WORLD_READABLE and MODE_WORLD_WRITABLE if it fits your needs.
You can also provide a file name as the first parameter if needed :
SharedPreferences sharedPreferences = getPreferences("com.example.stackoverflow.myfile", Context.MODE_PRIVATE);
Write a shared pref
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("USERNAME", "test");
editor.commit();
You can put any primitive type : int, string, boolean, etc.
It is a key/value set. the key string "USERNAME" will then have a value of "test".
Read shared pref
String username = sharedPreferences.getString("USERNAME", "NO NAME");
The second parameter is a default value to use if the key "USERNAME" didn't get any value.

Retrieving SharedPreferences

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

How to have android app remember user data/profile?

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.

storing password using shared preference in android

I am using shared preference to store password but when ever my application starts it again ask preference value .i want once i have entered the preference password it should be fixed it should ask me again and again.
Where you want save your password use below code -
Editor editor = getSharedPreferences("password", 0).edit();
editor.putString("password", "your password");
editor.commit();
And where you like to get it back put below code there -
SharedPreferences pref = getSharedPreferences("password", 1);
String password = pref.getString("password", "");
If still its not working, Put your code out here.

Categories

Resources