从片段获取非活动类中的SharedPreference值

时间:2019-07-05 07:22:24

标签: android fragment sharedpreferences

我正在尝试访问非活动类中的SharedPreferences值。 Preferences被定义并保存在片段中。我从搜索栏获得三个值,红色,绿色和蓝色分别对应一个。搜寻栏从0变为255,SharedPreferences正常工作,退出应用程序时保存了值。

我正在尝试获取其他类中的值,然后通过POST请求发送它们,但是我确定如何获取它们。你能帮我吗?

片段:

public class SettingsFragment extends Fragment {

public static SharedPreferences preferences;
    public static SharedPreferences.Editor editor;

    public static final String RED_PROG = "RED_BAR";
    public static int redValue;

    public static final String GREEN_PROG = "GREEN_BAR";
    public static int greenValue;

    public static final String BLUE_PROG = "BLUE_BAR";
    public static int blueValue;

    public static final String DIMMER_PROG = "DIMMER_BAR";
    public static int dimmerValue;

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_settings, container, false);

        preferences = getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);

        getRedBar();
        getGreenBar();
        getBlueBar();

        return view;
    }

    public void onResume() {
        super.onResume();

        redBar.setProgress(preferences.getInt(RED_PROG,0));
        greenBar.setProgress(preferences.getInt(GREEN_PROG,0));
        blueBar.setProgress(preferences.getInt(BLUE_PROG,0));
        dimmerBar.setProgress(preferences.getInt(DIMMER_PROG,0));
    }

    public void onPause() {
        super.onPause();

        editor = preferences.edit();

        redValue = redBar.getProgress();
        greenValue = greenBar.getProgress();
        blueValue = blueBar.getProgress();
        dimmerValue = dimmerBar.getProgress();

        editor.putInt(RED_PROG, redValue);
        editor.putInt(GREEN_PROG, greenValue);
        editor.putInt(BLUE_PROG, blueValue);
        editor.putInt(DIMMER_PROG, dimmerValue);

        editor.commit();
    }
}

其他类(颜色值应在RgbCapability中插入零):

public class ContextCommunication {

    public static void sendPref(){

      RgbCapability rgbCapability = new RgbCapability(0,0,0);
      Light light = new Light(true,rgbCapability, 0);
      Post post = new Post("Zagreb", light);

      Call<Post> call = MainActivity.apiService.savePost(post);

      textViewResult.setText(post.toString());

      call.enqueue(new Callback<Post>() {
          @Override
          public void onResponse(Call<Post> call, Response<Post> response) {

              if (!response.isSuccessful()) {
                  textViewResult.setText(response.code());
                  return;
              }

              Post postResponse = response.body();

              String content = "";
              content += "Code: " + response.code() + "\n";
              content += "location: " + postResponse.getLocation() + "\n";
              content += "light: " + postResponse.getLight() + "\n";


              //textViewResult.setText(content);
          }

          @Override
          public void onFailure(Call<Post> call, Throwable t) {
              textViewResult.setText(t.getMessage());
          }
      });
  }
}

3 个答案:

答案 0 :(得分:0)

从首选项中获取实例

sharedPreferences = getActivity().getSharedPreferences(preferences, 
Activity.MODE_PRIVATE);

保存为首选项

sharedPreferences.edit().putBoolean(key, value).apply();

从首选项中获取

sharedPreferences.getBoolean(key, defaultValue);

答案 1 :(得分:0)

  

我正在尝试访问非活动类中的SharedPreferences值

sharedPreferences = textViewResult.getContext().getSharedPreferences(preferences, 
Activity.MODE_PRIVATE);
int redValue=sharedPreferences.getInt(RED_PROG,0);
...

如果sendPref方法中的Context in不可用,则将其传递给访问SharedPreferences实例。

如果Api级别> 8,则使用apply()代替commit()

答案 2 :(得分:0)

您可以将getContext()作为参数从片段传递到非活动类。使用上下文可以获取共享的首选项。

public static void sendPref(Context context) {
    context.getSharedPreference..... 
}