多用户

时间:2017-03-31 12:21:52

标签: android android-sharedpreferences multi-user

在我的应用中,存储数据我正在使用SharedPreferences。我能够从SharedPreferences添加和检索数据。我的问题是当我切换到New user时,SharedPreferences没有为new user创建。始终从owner获取数据而不是从Newuser获取数据。如何创建SharedPreferences,以便创建ownerNewuser

由于

2 个答案:

答案 0 :(得分:0)

像这样使用。

   SharedPreferences.Editor editor = getSharedPreferences("first user",         MODE_PRIVATE).edit();
    editor.putString("firstusername", "bcd");
     editor.putInt("password", 12);
       editor.commit();

   //for seconduser.

   SharedPreferences.Editor editor2 = getSharedPreferences("second user", MODE_PRIVATE).edit();
  editor2.putString("secondusername", "abc");
  editor2.putInt("password", 12);

  editor.commit();

//用于翻新。

      SharedPreferences prefs = getSharedPreferences("SharedPreferences   prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
   String restoredText = prefs.getString("firstusername", null);
   if (restoredText != null) {
    String name1 = prefs.getString("firstusername", "No name defined");

//    for seconduser retriving
SharedPreferences prefs = getSharedPreferences("SharedPreferences   prefs = getSharedPreferences("secon user", MODE_PRIVATE); 
   String restoredText = prefs.getString("secondtusername", null);
   if (restoredText != null) {
    String name2 = prefs.getString("secondtusername", "No name defined");

}

答案 1 :(得分:0)

您可以使用用户标识来区分所有者和新用户。如果没有,那么你必须保持它。

您可以使用以下单个SharedPreferences util类来处理所有问题。我做了singleton并使用enum来确保type-safety


public class SharedPreferencesUtil
{
    public enum KEY
    {
        FIRST_TIME_USER("first_time"),
        REGISTERED_USER("registered"),
        SKIPPED_REGISTRATION("is_skipped_registration"),
        DEVICE_INFO("device_information"),
        DEVICE_LATITUDE("device_latitude"),
        DEVICE_LONGITUDE("device_longitude"),
        ONLINE_APPS("online_apps");

        private final String value;

        KEY(String value)
        {
           this.value = value;
        }

        public String getValue()
        {
            return this.value;
        }
    }

    private static SharedPreferencesUtil sharedPreferencesUtil = null;
    private static SharedPreferences sharedPreferences = null;

    private SharedPreferencesUtil(Context context, String spName)
    {
        sharedPreferences = QCleaner.getInstance().getActivity().getSharedPreferences("mobi_pref", Context.MODE_PRIVATE);
    }

    public static synchronized SharedPreferencesUtil getInstance(Context context, String spName)
    {
        if(sharedPreferencesUtil == null)
        {
            sharedPreferencesUtil = new SharedPreferencesUtil(context, pName);
        }

        return sharedPreferencesUtil;
    }

    public boolean hasKey(KEY key)
    {
        boolean isKeyExist = false;

        /*if(sharedPreferences.contains(key.getValue()) && !TextUtils.isEmpty(sharedPreferences.getString(key.getValue(), "")))
        {
            isKeyExist = true;
        }*/

        if(sharedPreferences.contains(key.getValue()))
        {
            isKeyExist = true;
        }

        return isKeyExist;
    }

    public boolean saveString(KEY key, String value)
    {
        return sharedPreferences.edit().putString(key.getValue(), value).commit();
    }

    public boolean saveInt(KEY key, int value)
    {
        return sharedPreferences.edit().putInt(key.getValue(), value).commit();
    }

    public boolean saveBoolean(KEY key, boolean value)
    {
        return sharedPreferences.edit().putBoolean(key.getValue(), value).commit();
    }

    public boolean saveFloat(KEY key, float value)
    {
        return sharedPreferences.edit().putFloat(key.getValue(), value).commit();
    }

    public boolean saveDataLong(KEY key, long value)
    {
        return sharedPreferences.edit().putLong(key.getValue(), value).commit();
    }

    /*public boolean saveDataStringSetInSharedPreference(String key, Set value)
    {
        return sharedPreferences.edit().putStringSet(key, value).commit();
    }*/

    public String getString(KEY key)
    {
        if(sharedPreferences.contains(key.getValue()))
        {
            return sharedPreferences.getString(key.getValue(), "");
        }
        else
        {
            return null;
        }
    }

    public boolean getBoolean(KEY key)
    {
        if(sharedPreferences.contains(key.getValue()))
        {
            return sharedPreferences.getBoolean(key.getValue(), false);
        }
        else
        {
            return false;
        }
    }

    public float getFloat(KEY key)
    {
        if(sharedPreferences.contains(key.getValue()))
        {
            return sharedPreferences.getFloat(key.getValue(), 0.0f);
        }
        else
        {
            return 0.0f;
        }
    }

    public float getLong(KEY key)
    {
        if(sharedPreferences.contains(key.getValue()))
        {
            return sharedPreferences.getLong(key.getValue(), 0L);
        }
        else
        {
            return 0L;
        }
    }

    public boolean removeKey(KEY key)
    {
        boolean isDeleted = false;
        if(sharedPreferences.contains(key.getValue()))
        {
            isDeleted = sharedPreferences.edit().remove(key.getValue()).commit();
        }

        return isDeleted;
    }
}

如何使用?

SharedPreferencesUtil.getInstance(context, your_shared_preference_name).saveString(SharedPreferencesUtil.KEY.ONLINE_APPS, object.toString());

如果您使用单一名称共享偏好设置,则可以对其进行自定义,而不必每次都传递contextsharedpreference名称。