Reputation: 175
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!
Upvotes: 1
Views: 65
Reputation: 1
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.
Upvotes: 0
Reputation: 1691
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.
Upvotes: 1