从SharedPreferences获取价值

时间:2017-10-28 16:25:35

标签: java android sharedpreferences

我想通过此Settings.getMode();从活动中获取已保存的值。这是Settings类:

public class Set extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

private static String isNightMode;
Switch aSwitch;
Boolean mode;

@Override
protected void onCreate(Bundle savedInstanceState) {

    SharedPreferences sharedPreferences = getSharedPreferences("xyz", MODE_PRIVATE);
    isNightMode = sharedPreferences.getString("isNightMode", "no");
    mode = sharedPreferences.getBoolean("mode", false);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_set);

    aSwitch = (Switch) findViewById(R.id.switch1);
    aSwitch.setChecked(mode);

    if (aSwitch.isChecked()) {
        isNightMode = "yes";
    } else {
        isNightMode = "no";
    }

    aSwitch.setOnCheckedChangeListener(this);
}

public static String getMode() {
    return isNightMode;
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked) {
        isNightMode = "yes";
        mode = true;
        SharedPreferences.Editor editor = getSharedPreferences("xyz", MODE_PRIVATE).edit();
        editor.putString("isNightMode", isNightMode);
        editor.putBoolean("mode", mode);
        editor.commit();

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    } else {
        isNightMode = "no";
        mode = false;
        SharedPreferences.Editor editor = getSharedPreferences("xyz", MODE_PRIVATE).edit();
        editor.putString("isNightMode", isNightMode);
        editor.putBoolean("mode", mode);
        editor.commit();

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }
}
}

当我更改Settings活动中的值时,该值将完美保存。但问题是,我没有在应用程序启动时获得保存的值。应用程序启动时,getMode()方法返回null。在启动应用程序时获取保存的值时一定有问题。我无法弄清楚错误。

2 个答案:

答案 0 :(得分:0)

1.您可以使用using

2.并在您的活动中致电MyApplication

试试这个。

MyApplication.getMode();

并添加public class MyApplication extends Application { private static String isNightMode; Boolean mode; @Override public void onCreate() { super.onCreate(); SharedPreferences sharedPreferences = getSharedPreferences("xyz", MODE_PRIVATE); isNightMode = sharedPreferences.getString("isNightMode", "no"); mode = sharedPreferences.getBoolean("mode", false); } public static String getMode() { return isNightMode; } }

manifest

答案 1 :(得分:0)

此修改可能对您有所帮助。 onCreate()方法中的修改:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); // this - Activity

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit(); // this - Activity

    if (isChecked) {
        isNightMode = "yes";
        mode = true;                       

    } else {
        isNightMode = "no";
        mode = false;
    }

    editor.putString("isNightMode", isNightMode);
    editor.putBoolean("mode", mode);
    editor.apply();

    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
}