13

有没有办法在我的整个应用程序中使 SharedPreferences 全球化?现在,我在整个代码中的很多地方都使用这些行来存储我的用户可以设置的许多首选项的简单开/关设置。如果可能的话,我只想在全球范围内给他们打一次电话:

SharedPreferences settings = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = settings.edit();

关于如何在所有类中只调用这些行的任何提示都会很棒:

editor.putString("examplesetting", "off");
editor.commit(); 

String checkedSetting = settings.getString("examplesetting", "");
4

4 回答 4

19

我知道,我知道,我会发炎,被扔进地狱的余烬中……

使用包含设置的单例类SharedPreference.. 像这样:

public class PrefSingleton{
   private static PrefSingleton mInstance;
   private Context mContext;
   //
   private SharedPreferences mMyPreferences;

   private PrefSingleton(){ }

   public static PrefSingleton getInstance(){
       if (mInstance == null) mInstance = new PrefSingleton();
       return mInstance;
   }

   public void Initialize(Context ctxt){
       mContext = ctxt;
       //
       mMyPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
   }
}

并围绕您的示例在问题中表示的内容创建包装函数,例如,

PrefSingleton.getInstance().writePreference("exampleSetting", "off");

并且实现可能是这样的:

// Within Singleton class

public void writePreference(String key, String value){
     Editor e = mMyPreference.edit();
     e.putString(key, value);
     e.commit();
}

从您的第一个活动开始,以这种方式激活单例类,如下所示:

PrefSingleton.getInstance().Initialize(getApplicationContext());

我冒险投反对票的原因是,使用全局静态类可能是一个坏主意,并且违背了编程基础的实践。但是话虽如此,除了挑剔之外,它将确保只有一个也是唯一的类对象PrefSingleton可以存在并且无论代码处于什么活动都可以访问。

于 2012-07-19T01:16:15.780 回答
11

我会扩展Application并包含SharedPreferences.Editor一个带有吸气剂的字段。

public class APP extends Application {
    private final SharedPreferences settings = getSharedPreferences("prefs", 0);
    private final SharedPreferences.Editor editor = settings.edit();

    public SharedPreferences.Editor editSharePrefs() {
        return editor;
    }
}

然后你可以从任何访问Activity

((APP) getApplication()).editSharePrefs().putString("examplesetting", "off");
((APP) getApplication()).editsharePrefs().commit();

或者,您也可以在方法中抛出

    public static APP getAPP(Context context) {
        return (APP) context.getApplicationContext();
    }

虽然,这只会改变您拨打的电话

APP.getAPP(this).editSharePrefs().putString("examplesetting", "off");
APP.getAPP(this).editsharePrefs().commit();

所以这真的是个人喜好,对你来说看起来更干净。

于 2012-07-19T01:29:03.097 回答
3

我会这样做:

public class BaseActivity extends Activity {

        protected boolean setInHistory;
        protected SharedPreferences sharedPrefs;

            @Override
        protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    setInHistory = true;
       }


}


public class MainActivity extends BaseActivity {

         public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

              System.out.println(setInHistory+" <-- setInhistory");

          }
}

然后可以访问 sharedPrefs,因为它受到保护,然后可以通过包访问。

In the console you have : true <-- setInhistory
于 2013-03-14T20:35:20.450 回答
0

使用 Helper 类来获得你想要的一切,使方法成为静态的。这就是 ADW 的做法。

于 2012-07-19T02:25:00.907 回答