Android - Using Shared Preferences in separate cla

2019-05-04 10:42发布

问题:

I want to save data using Shared Preferences in android. But I am looking to use separate class to do this task. I have implemented that class like below,

import android.content.Context;
import android.content.SharedPreferences;

public class SavePref {

    private Context context;

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

    public void saveInt(String key, int value) {

        SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putInt(key, value);
        editor.commit();

    }

}

But there is an error on getActivity(),

The method getActivity() is undefined for the type SavePref

How to solve this?
Thanks

回答1:

getActivity() is a method of Fragment not of your SavePref. In your case the simple fix is to use the Context you are keeping as member variable to retrieve the SharedPreferences. An alternative to your approach is to avoid keeping the context as member variable, linking somehow the shared preferences to an instance of of your SavePref class, and have a static method

  public static void saveInt(Context context, String key, int value) {
        SharedPreferences sharedPref = context.getDefaultSharedPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putInt(key, value);
        editor.commit();
    }

and address the method like:

SavePref.saveInt(getActivity(), key, value);

from a Fragment or

SavePref.saveInt(this, key, value);

from an Activity. This way you don't need to instantiate SavePref every time you need to call saveInt, and you can avoid to store a reference to the Context.



回答2:

change

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);

to

SharedPreferences sharedPref = context.getPreferences(Context.MODE_PRIVATE);

You already have context then why should you used getActivity()?

Actually getActivity() is used to get context in Fragment.



回答3:

I have done a class to do the same thing. This way you can save Boolean, int or string data in a specific group of preferences for your app.

I hope it can helps you

import android.content.Context;
import android.content.SharedPreferences;

public class DataProccessor {

    private static Context context;

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

    public final static String PREFS_NAME = "appname_prefs";

    public static void setInt( String key, int value) {
        SharedPreferences sharedPref = context.getSharedPreferences(PREFS_NAME,0);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public static int getInt(String key) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
        return prefs.getInt(key, 0);
    }

    public static void setStr(String key, String value) {
        SharedPreferences sharedPref = context.getSharedPreferences(PREFS_NAME,0);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString(key, value);
        editor.apply();
    }

    public static String getStr(String key) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
        return prefs.getString(key,"DNF");
    }

    public static void setBool(String key, boolean value) {
        SharedPreferences sharedPref = context.getSharedPreferences(PREFS_NAME,0);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public static boolean getBool(String key) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
        return prefs.getBoolean(key,false);
    }
}

To use it I'll show you a little example with a String value

For an Activity

 //// To Save a value
 DataProccessor dataProccessor = new DataProccessor(this);
 dataProccessor.setStr("email","johndoe@mail.com");

 //// To Retreive a value
 dataProccessor.getStr("email");

For a Fragment

 //// To Save a value
 DataProccessor dataProccessor = new DataProccessor(getActivity());
 dataProccessor.setStr("email","johndoe@mail.com");

 //// To Retreive a value
 dataProccessor.getStr("email");