20

我的应用程序中有一个用户偏好,它被不同的活动使用。我想知道在我的应用程序中的不同活动之间利用这些偏好的最佳方式。

我的想法是从主要活动创建一个共享偏好对象,然后从那里将意图发送到不同的活动以采取行动。这能行吗……?

或者只是继续从每项活动中调用getsharedpreferences()..?

谢谢。

4

4 回答 4

26

通过意图发送共享偏好似乎过于复杂。您可以使用以下内容包装共享首选项,并直接从您的活动中调用方法:

public class Prefs {
    private static String MY_STRING_PREF = "mystringpref";
    private static String MY_INT_PREF = "myintpref";

    private static SharedPreferences getPrefs(Context context) {
        return context.getSharedPreferences("myprefs", 0);
    }

    public static String getMyStringPref(Context context) {
        return getPrefs(context).getString(MY_STRING_PREF, "default");
    }

    public static int getMyIntPref(Context context) {
        return getPrefs(context).getInt(MY_INT_PREF, 42);
    }

    public static void setMyStringPref(Context context, String value) {
        // perform validation etc..
        getPrefs(context).edit().putString(MY_STRING_PREF, value).commit();
    }

    public static void setMyIntPref(Context context, int value) {
        // perform validation etc..
        getPrefs(context).edit().putInt(MY_INT_PREF, value).commit();
    }
}
于 2010-10-29T12:48:46.193 回答
8

您可以使用这种方式并在要使用的所有活动中声明具有相同名称的相同变量。

  public static final String PREFS_NAME = "MyPrefsFile";
  static SharedPreferences settings;
  SharedPreferences.Editor editor;
  int wordCount;

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    settings = getSharedPreferences(PREFS_NAME, 0);
    editor = settings.edit();

    wordCount = settings.getInt("wordCount", 4); 

  }

这里最初 wordCount 将给出 4;当您编辑 wordCount 并想再次存储时

  editor.putInt("wordCount", 6);
  editor.commit();

您必须在要使用共享首选项的活动中声明相同的变量。在每个活动中调用 getSharedPreferences 会更好。

我认为将这种偏好传递给意图是行不通的。

于 2010-10-29T12:41:26.020 回答
0

您当然可以在应用程序中使用共享首选项。

如果您有比字符串或 int 更多的简单类型,则可以使用单例或扩展应用程序类,应用程序的所有活动都可以访问该类。=> 这里没有磁盘访问。简单地留在记忆中。

于 2010-10-29T12:44:12.670 回答
0

这是Kotlin中一个不错且简单的解决方案——只需将代码复制并粘贴到一个新AppPreferences.kt文件中,然后按照代码中列出的 4 个 TODO 步骤进行操作:

import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.content.SharedPreferences
import androidx.core.content.edit

object AppPreferences {
    private var sharedPreferences: SharedPreferences? = null

    // TODO step 1: call `AppPreferences.setup(applicationContext)` in your MainActivity's `onCreate` method
    fun setup(context: Context) {
        // TODO step 2: set your app name here
        sharedPreferences = context.getSharedPreferences("<YOUR_APP_NAME>.sharedprefs", MODE_PRIVATE)
    }

    // TODO step 4: replace these example attributes with your stored values
    var heightInCentimeters: Int?
        get() = Key.HEIGHT.getInt()
        set(value) = Key.HEIGHT.setInt(value)

    var birthdayInMilliseconds: Long?
        get() = Key.BIRTHDAY.getLong()
        set(value) = Key.BIRTHDAY.setLong(value)

    private enum class Key {
        HEIGHT, BIRTHDAY; // TODO step 3: replace these cases with your stored values keys

        fun getBoolean(): Boolean? = if (sharedPreferences!!.contains(name)) sharedPreferences!!.getBoolean(name, false) else null
        fun getFloat(): Float? = if (sharedPreferences!!.contains(name)) sharedPreferences!!.getFloat(name, 0f) else null
        fun getInt(): Int? = if (sharedPreferences!!.contains(name)) sharedPreferences!!.getInt(name, 0) else null
        fun getLong(): Long? = if (sharedPreferences!!.contains(name)) sharedPreferences!!.getLong(name, 0) else null
        fun getString(): String? = if (sharedPreferences!!.contains(name)) sharedPreferences!!.getString(name, "") else null

        fun setBoolean(value: Boolean?) = value?.let { sharedPreferences!!.edit { putBoolean(name, value) } } ?: remove()
        fun setFloat(value: Float?) = value?.let { sharedPreferences!!.edit { putFloat(name, value) } } ?: remove()
        fun setInt(value: Int?) = value?.let { sharedPreferences!!.edit { putInt(name, value) } } ?: remove()
        fun setLong(value: Long?) = value?.let { sharedPreferences!!.edit { putLong(name, value) } } ?: remove()
        fun setString(value: String?) = value?.let { sharedPreferences!!.edit { putString(name, value) } } ?: remove()

        fun remove() = sharedPreferences!!.edit { remove(name) }
    }
}
于 2020-01-05T13:59:49.797 回答