我希望我的应用能够记住或保存用户在我的应用中获得的硬币数量,这样每当他们再次打开我的应用时,他们就可以看到他们的硬币而不是0。
答案 0 :(得分:2)
您应该在Android上使用共享偏好设置。
我写下了一个共享偏好应用程序的功能完整的示例。这个是从我的项目中获取的(链接代码)。
首先,您应该创建一个类(假设您的“硬币”将用于更多活动)。 这个名为 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);
}
}
在其他活动:
中使用此课程 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是我项目的来源。
Here是教程点的实用链接。
Here是Android开发者的官方指南。
答案 1 :(得分:1)
在Android上存储数据的方式有很多种。阅读this guide并确定哪一个更适合您的任务。
答案 2 :(得分:-1)
使用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");
}
}
数据是根据类的完全限定名称存储的,因此您的包名称和类名称是相关的。来自Preferences类的文档:
此类允许应用程序存储和检索用户和系统首选项和配置数据。此数据持久存储在依赖于实现的后备存储中。典型的实现包括平面文件,特定于操作系统的注册表,目录服务器和SQL数据库。此类的用户不必关心后备存储的详细信息。