【问题标题】:Storing session state in android application在android应用程序中存储会话状态
【发布时间】:2013-12-23 13:56:09
【问题描述】:

我知道这个问题比较频繁,但看来我还是得问一下。

我正在开发一款网络游戏。该应用程序保存当前用户的帐户信息、团队信息、单位、私人消息等,以提供对它们的永久访问。我创建了一个包含所有这些的类,现在我每次在应用程序启动时加载会话数据,显示启动活动。会话数据实例位于Application 子类中。

每次,在每个 Activity 的 onCreateonResume 中,我都会检查会话数据是否处于活动状态,如果不是,则启动启动活动以重新加载它。我知道这种方法是一种不好的做法,但它看起来是管理会话的最快方法。

最近,尽管我进行了onCreateonResume 的检查,但我已经开始对我的应用进行 beta 测试,并且在针对会话数据对象的对象中面临大量 NPE。

有人可以提出更好的解决问题的方法吗?

另外,

  • 会话数据需要存储大量的对象类型,所以为它们创建所有的数据库真的很繁琐;
  • 服务器上的会话数据经常被修改,所以加载一次就永远使用并不是一个好主意。

【问题讨论】:

    标签: android session architecture


    【解决方案1】:

    SharedPreferences是解决这类问题的好方法..

    下面的代码显示了一个可以用来保存和获取用户数据的类

        public class SharedPreferencesClass {
        public static final String PREFS_NAME = "MyPrefsFile";
        public static final int PRIVATE_MODE = 0;
        public SharedPreferences setting;
        public SharedPreferences.Editor editor;
    
        public SharedPreferencesClass(Context context) {
            setting = context.getSharedPreferences(PREFS_NAME, PRIVATE_MODE);
        }
    
        //Save user data by calling this method
        public void setLoginData(String userId, String userName, String email,
                String image_str, String about) {
            editor = setting.edit();
            editor.putBoolean("Login", true);
            editor.putString("UserID", userId);
            editor.putString("UserName", userName);
            editor.putString("Email", email);
            editor.putString("About", about);
            editor.commit();
        }
    
        //An example of editing some of the user data
        public void setEditedData(String email, String image_str ,String about) {
            editor = setting.edit();
            editor.putString("Email", email);
            editor.putString("About", about);
            editor.commit();
        }
    
        //this method for clearing all of the user information
        public void clearPerferance() {
            editor = setting.edit();
            editor.clear();
            editor.commit();
        }
    
        //The below methods is for getting specific data that you saved it the shared preference ...
    
        public boolean isLogin() {
            return setting.getBoolean("Login", false);
        }
    
        public String getUserName() {
            return setting.getString("UserName", "");
        }
    
        public String getUserID() {
            return setting.getString("UserID", "");
        }
    
        public String getEmail() {
            return setting.getString("Email", "");
        }
    
        public String getAbout() {
            return setting.getString("About", "");
        }
    
    }
    

    这里还有一个tutorial,它解释并展示了如何使用SharedPreferences

    【讨论】:

    • 每个单位大约有50个技能,每个团队大约有30个单位,加上账号信息等等。你真的认为共享偏好是保存所有这些的正确位置吗?
    • 共享首选项不是存储在 XML 中吗?
    • SharedPreferences 我认为它有利于保存少量数据(如帐户信息和其他首选项选项),这并不意味着您不能保存大量数据.. 但如果您真的有要保存的大量数据,您必须使用 SQLite 数据库...下面的链接也可以帮助您找到最佳存储选项,具体取决于您要保存的数据量。 developer.android.com/guide/topics/data/data-storage.html
    【解决方案2】:

    不要每次都启动启动画面。它会降低应用程序的性能,只需创建一种方法来加载会话数据并在会话不存在时调用它。并在应用程序的共享首选项中加载会话数据。

    【讨论】:

    • 为什么会降低性能?无论如何,用户必须等待大约 30 秒才能加载会话数据,不是吗?
    • 每次启动闪屏都会降低应用程序的性能。如果不调用单独的方法再次加载会话,只需检查会话是否处于活动状态。
    【解决方案3】:

    您需要将对象存储到 SharedPreferences。据我所知,您不能对SharedPreferences 提出异议。所以你需要做一些技巧。我认为这个解决方案对你来说已经足够了:

    SharedPreferences  mPrefs = getPreferences(MODE_PRIVATE);
    

    保存

     Editor prefsEditor = mPrefs.edit();
     Gson gson = new Gson();
     String json = gson.toJson("MyObject");
     prefsEditor.putString("MyObject", json);
     prefsEditor.commit();
    

    检索

    Gson gson = new Gson();
    String json = mPrefs.getString("MyObject", "");
    MyObject obj = gson.fromJson(json, MyObject.class);
    

    此代码来自 "this quesiton"MuhammadAamirALi

    【讨论】:

    • Json 相当慢,不是吗?
    • 如何将数据从服务器发送到客户端?我建议首先检查从服务器向客户端发送 json 并解析它需要多少时间。您可以使用 Logcat 进行检查。 Log.w("----DEBUG----", "<current place>")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    • 2012-01-26
    • 2011-10-15
    相关资源
    最近更新 更多