问题
I want my app to remember or save the number of coins that users earn in my app so that whenever they open my app again they can see their coins instead of a 0.
回答1:
You should use shared preferences on Android.
I write down a fully functional example of an application of a shared preferences. This one is taken from my project (link down the code).
First, you should create a class (supposing that your "coins" will be used in more activities). This one is called SessionManager.java:
public class SessionManager {
// Shared Preferences
SharedPreferences pref;
// Editor for Shared preferences
Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Sharedpref file name
private static final String PREF_NAME = "FoodAdvisorPref";
// All Shared Preferences Keys
private static final String IS_LOGIN = "IsLoggedIn";
public static final String KEY_AZIENDA = "aziendaName";
public static final String KEY_EMAIL = "email";
public static final String KEY_NAME = "name";
public static final String KEY_SECOND_NAME = "second_name";
public static final String KEY_DESCRIPTION = "description";
public static final String KEY_ID = "id";
public static final String KEY_PHOTO = "photo";
// Constructor
public SessionManager(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
/**
* Create login session
* */
public void createLoginSession(String azienda,String name,String second_name, String email,String description,String id,String photo){
// Storing login value as TRUE
editor.putBoolean(IS_LOGIN, true);
editor.putString(KEY_NAME, name);
editor.putString(KEY_EMAIL, email);
editor.putString(KEY_AZIENDA,azienda);
editor.putString(KEY_SECOND_NAME,second_name);
editor.putString(KEY_DESCRIPTION,description);
editor.putString(KEY_PHOTO,photo);
editor.putString(KEY_ID,id);
// commit changes
editor.commit();
}
/**
* Check login method wil check user login status
* If false it will redirect user to login page
* Else won't do anything
* */
public void checkLogin(){
// Check login status
if(!this.isLoggedIn()){
// user is not logged in redirect him to Login Activity
Intent i = new Intent(_context, LoginActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
}
//session data like hash
public HashMap<String, String> getUserDetails(){
HashMap<String, String> user = new HashMap<String, String>();
user.put(KEY_NAME, pref.getString(KEY_NAME, null));
user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
user.put(KEY_AZIENDA, pref.getString(KEY_AZIENDA, null));
user.put(KEY_SECOND_NAME, pref.getString(KEY_SECOND_NAME, null));
user.put(KEY_DESCRIPTION, pref.getString(KEY_DESCRIPTION, null));
user.put(KEY_ID, pref.getString(KEY_ID, null));
user.put(KEY_PHOTO,pref.getString(KEY_PHOTO,null));
return user;
}
//close session
public void logoutUser(){
// Clearing all data from Shared Preferences
editor.clear();
editor.commit();
// After logout redirect user to Loing Activity
Intent i = new Intent(_context, LoginActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Starting Login Activity
_context.startActivity(i);
}
// Get Login State
public boolean isLoggedIn(){
return pref.getBoolean(IS_LOGIN, false);
}
}
To use this class in the others activities:
SessionManager session; //as global variable if you want
session = new pro.rane.foodadvisor.SessionManager(getApplicationContext()); //inside create view
session.checkLogin();
// get user data from session
HashMap<String, String> user = session.getUserDetails();
// name example
String name = user.get(SessionManager.KEY_NAME);
// email example
String email = user.get(SessionManager.KEY_EMAIL);
Here is the source of my project.
Here is a practical link by tutorials point.
Here is the official guide for Android developers.
回答2:
There're different ways to store data on Android. Read this guide and decide which one is more suitable for you task.
回答3:
Use the Java Preferences API.
import java.util.prefs.*;
public class Example {
// Preference key
private static final String COIN = "coin";
public void savePreference(String coinValue) {
Preferences prefs = Preferences.userNodeForPackage(Example.class);
prefs.put(Coin, coinValue);
}
public String readPreference() {
Preferences prefs = Preferences.userNodeForPackage(Example.class);
return prefs.get(COIN, "default");
}
}
The data is stored based on the fully-qualified name of your class, so your package name and class name are relevant. From the documentation for the Preferences class:
This class allows applications to store and retrieve user and system preference and configuration data. This data is stored persistently in an implementation-dependent backing store. Typical implementations include flat files, OS-specific registries, directory servers and SQL databases. The user of this class needn't be concerned with details of the backing store.
来源:https://stackoverflow.com/questions/44246801/how-can-i-make-my-app-remeber-save-the-coins-my-app-users-earned-my-app