How do I differentiate between 2 users using shared preferences? - android

I am trying to differentiate between 2 user id's using sharedpreferences so preferences saved by one should not be seen by the other logging on the same device.
I plan on doing something liek this:
Sharedpreferences.java
public boolean isDifferentUser() {
return mSharedPreferences.getBoolean(ISDIFFERENTUSER, false);
}
I want to see if I can apply some sort of logic such that
if(isdifferentuser){
dont save preferences
}
but I dont know how to differentiate between 2 users. They have 2 different user id's stored in database, how do I go about it?
Any pointers?
Thanks!

Related

Storing Preferences on a multi user app

I am developing an android application. It has two types of users, a teacher and a student. I was hoping to use Shared Preferences to communicate between the two users.
Basically, I have a teach screen which has four TextViews on it with different messages. The TextView text is set to "" when the toggle value is 1 (which is the default value).
On the student screen, they have 4 Buttons. Upon hitting each button, the toggle value is changed to 0 and then the message is displayed on the teacher's view. This works when I log in as a student, click the button, then log in as a teacher. However, I want it to work on two different devices, so that when the student clicks the button the teacher sees the message.
Here are some code snippets:
Firstly, the student activity:
Assistance.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
toggleAssistance(view);
}
});
public void toggleAssistance(View view){
SharedPreferences sharedPreferences = getSharedPreferences("requestToggles", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("assistanceToggle", 0);
editor.apply();
}
Secondly, the teacher activity:
asstToggle = sharedPreferences.getInt("assistanceToggle", 1);
if(asstToggle==1){
Assistance.setText("");
clearAssistance.setVisibility(View.GONE);
}
Clear assistance is just a button that basically does the reverse of toggleAssistance (it sets the value to 1). No need for this button to be displayed when the message isn't.
Can anyone provide assistance or advice in to how I can make this work between devices? Thanks!
No, you cannot use shared preferences to communicate between devices, your preferences values are stored locally on the device. You should use a remote data storage system like Firebase to implement a solution where two different devices are interacting with each other.
In the real professional world, What you develop in the android environment is considered a Front End.
To create a communication between 2 users(2 different android environments/machines), you need a back-end environment.
If you want to store data that can be accessed by 2 or more android users of different machines, you need the data stored in The "Back End". The most popular database system at the moment is MySQL.

How to save number of times a user has tapped on a view

I am designing a simple application that will count how many times a user has tapped on a imageView. My question is what would be the best way of saving and reading this file. Any suggestions? I am thinking something like using Parse.com's local database. I have tried it, but I could not get it working the way I wanted. I am still a beginner, so please not so fancy suggestions.
Try to save data in SharedPreference. SharedPreference works like database for application on device that will be stored until any one has unistall app from device.
To create sharedPrefernce-
SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
To store data -
prefs.edit().putInt("key", int_value).apply();
To retrieve data-
// use a default value
int l = prefs.getLong("key", default_value);
Simplest options is always thebest option, go with shared preferences
Here is simple tutorial from google http://developer.android.com/training/basics/data-storage/shared-preferences.html
It will store your data in application local file. Take a note of that there are different shared preferences in example getPreferences() will return file specific for activity you used this method. While getSharedPreferences() will return application global file.

How to Know the user has ever used the app

How can I know if the user has ever used my Android app on this device
before?
You can create these methods to manipulate SharedPreferences:
Keep in mind, there is no way to stop the user from clearing the app data and therefore deleting the SharedPreference.
public boolean hasUsed()
{
SharedPreferences sp = getSharedPreferences("hasUsed",0);
boolean b = sp.getBoolean("hasUsed",false); // default value is false
return b;
}
public void writeUsed(boolean b)
{
SharedPreferences.Editor editor = getSharedPreferences("hasUsed",0).edit();
editor.putBoolean("hasUsed", b).commit();
}
Call them like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// if user hasn't used the app, write to the preference that they have.
if (!hasUsed()) writeUsed(true);
// later in code:
if (hasUsed()) // if the user has used the app
{
// do something
}
}
Links for explanation of code:
How to store User name and password details in strings.xml
How to save app settings?
How to keep information about an app in Android?
If you store anything regarding it in your app,user is Owner of his device . he can clear it anytime.
Kindly store this information on either your server or use some analytics integration.
But I would prefer to store on server because in your case,analytics might be overhead for you as you just want to identify the existing users.
Explaination:
When your app starts, send device's unique id to your server, so that when user starts the app for next time,it will again send unique id and at that time your server can perform check in database for existance of recently received unique id.
In such a manner you can identify that the user has already used your app or not and then you can modify your business logic accordingly.
I hope it will be helpful !!
Check this : The Google Analytics SDK for Android makes it easy for native Android developers to collect user engagement data from their applications.
You can do things like :
The number of active users are using their applications.
From where in the world the application is being used.
Adoption and usage of specific features.
Crashes and exceptions.
In-app purchases and transactions.
https://developers.google.com/analytics/devguides/collection/android/
You can create a shared preference and can update a count value every time the user opens the app, like
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("Count",Integer.valueof(myPrefs.getString("Count", null))+1) );
the code may have errors but the logic is right

Saving Shared Prefrences in Multiple instances

I would like to be able to save my users session or sharedPrefrences in a way that if the user kills the application and you start it it would look like this.
Button one = Start Activity with Blank Preferences
Button Two = List of Saved Sessions of Preferences and once clicked all put into the Starting activity.
Is this possible and if so how would I go about doing that?
Thank you!
Yes you can do that and it is good to use sharedPreferences if you just have to store some session variables. But if it is more, then go for database.
Do clear sharedPrefences in your application you need to do this:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(activity);
Editor editor = settings.edit();
editor.clear();
editor.commit();
For reading the preferences, you can keep a sharedPreference with the count for the seesions. While saveing the prefences, always save with the strings session1, session2, session3 etc. So, while accessing them based on count, prepare a loop and form the string and access all the session variables and show them.
The reason why I didnt suggest you to do getAll() for sharedPreference is that, you may save few other things in sharedPreference. So by forming strings yourself, while reading you can just get the sessions and not other data saved in sharedPreference.
I hope you understand what I meant
Is this possible
I would say yes, depending on exactly what you mean.
if so how would I go about doing that?
SharedPreferences has a couple different functions to do something like this, depending on exactly what you want. You can get a Map of all preferences that are stored after clicking Button2 with getAll() or a set of preferences with a certain String such as "userName" or something similar with getStringSet(). Play around with the functions it offers and see if it gives you what you are looking for.
Also take not of the warnings of these functions
Note that you must not modify the set instance returned by this call. The consistency of the stored data is not guaranteed if you do, nor is your ability to modify the instance at all.

Dynamic preferences for a variable number of profiles in an Android app

I am looking for a way to create dynamic preferences where I don't need to hard code the preference key and I could have a variable number of preferences.
Basically, my application will let the user create a multiple number of profiles and each of these profiles will save custom values for a fixed number of preferences.
So this way, the user does not have to change the preferences every time he wants this app to run differently, he can just switch the profile.
One way I think will work is by subclassing all the standard Preference classes and calling their setKey method with my custom preference key, containing the profile name, but this is ugly.
So is there a cleaner and more standards compliant way to do this?
Regards,
Dhruwat
You can save different preferences in a different file for each user using the getSharedPreferences method:
getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.
That way, you can do something like this:
SharedPreferences settings = getSharedPreferences("prefs_user_"+user_id, 0);
// from now on you can use use the normal way to set or get the preferences
I'm assuming you are using an id in order to identify them users.

Categories

Resources