在可运行对象中使用 SharedPreferences

Using SharedPreferences in a runnable

我需要在从 Runnable 派生的 class 中的 SharedPreferences 中存储一些数据。

似乎没有 context 就无法得到它。例如,以下将需要一个不可用于 Runnable 实例的上下文对象。

PreferenceManager.getDefaultSharedPreferences(Context context)

有什么方法可以让它在 Runnable 中工作,还是我应该只使用数据库来获取所有首选项。

如何将上下文存储在可运行对象的字段中并将其作为构造函数参数传递?这样它就可以重复使用了。

public class MyRunnable implemets Runnable{
    private Context context;

    public MyRunnable(Context context){
        this.context = context;
    }

    public void run(){
        /...
    }
}