How Can I Make My App Remeber/save The Coins My App Users Earned My App?
Solution 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:
publicclassSessionManager {
// Shared Preferences
SharedPreferences pref;
// Editor for Shared preferences
Editor editor;
// Context
Context _context;
// Shared pref modeintPRIVATE_MODE=0;
// Sharedpref file nameprivatestaticfinalStringPREF_NAME="FoodAdvisorPref";
// All Shared Preferences KeysprivatestaticfinalStringIS_LOGIN="IsLoggedIn";
publicstaticfinalStringKEY_AZIENDA="aziendaName";
publicstaticfinalStringKEY_EMAIL="email";
publicstaticfinalStringKEY_NAME="name";
publicstaticfinalStringKEY_SECOND_NAME="second_name";
publicstaticfinalStringKEY_DESCRIPTION="description";
publicstaticfinalStringKEY_ID="id";
publicstaticfinalStringKEY_PHOTO="photo";
// ConstructorpublicSessionManager(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
/**
* Create login session
* */publicvoidcreateLoginSession(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
* */publicvoidcheckLogin(){
// Check login statusif(!this.isLoggedIn()){
// user is not logged in redirect him to Login ActivityIntenti=newIntent(_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 hashpublic HashMap<String, String> getUserDetails(){
HashMap<String, String> user = newHashMap<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 sessionpublicvoidlogoutUser(){
// Clearing all data from Shared Preferences
editor.clear();
editor.commit();
// After logout redirect user to Loing ActivityIntenti=newIntent(_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 StatepublicbooleanisLoggedIn(){
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 sessionHashMap<String, String> user = session.getUserDetails();
// name exampleString name = user.get(SessionManager.KEY_NAME);
// email exampleString 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.
Solution 2:
There're different ways to store data on Android. Read this guide and decide which one is more suitable for you task.
Solution 3:
Use the Java Preferences API.
import java.util.prefs.*;
publicclassExample {
// Preference keyprivatestaticfinalStringCOIN="coin";
publicvoidsavePreference(String coinValue) {
Preferencesprefs= Preferences.userNodeForPackage(Example.class);
prefs.put(Coin, coinValue);
}
public String readPreference() {
Preferencesprefs= 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.
Post a Comment for "How Can I Make My App Remeber/save The Coins My App Users Earned My App?"