Android-在单独的类中使用共享首选项?

Gau*_*nde 3 java android android-intent android-activity

我想使用Android中的“共享首选项”保存数据。但是我希望使用单独的类来完成此任务。我已经像下面这样实现了该类,

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();

    }

}
Run Code Online (Sandbox Code Playgroud)

但是,上有一个错误getActivity()

The method getActivity() is undefined for the type SavePref
Run Code Online (Sandbox Code Playgroud)

如何解决呢?
谢谢

Bla*_*elt 6

getActivity()是一种Fragment不属于你的方法SavePref。在您的情况下,简单的解决方法是使用作为成员变量保留的Context来检索SharedPreferences。一种替代方法是避免将上下文保留为成员变量,以某种方式将共享的首选项链接到类的实例SavePref,并使用静态方法

  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();
    }
Run Code Online (Sandbox Code Playgroud)

并解决以下方法:

SavePref.saveInt(getActivity(), key, value);
Run Code Online (Sandbox Code Playgroud)

Fragment

SavePref.saveInt(this, key, value);
Run Code Online (Sandbox Code Playgroud)

Activity。这样,您无需在每次调用时都实例化SavePref saveInt,并且可以避免存储对的引用Context


Mis*_* JJ 5

我创建了一个类来做同样的事情。通过这种方式,您可以保存布尔值、整数、字符串数据或检查数据是否已保存在应用程序的特定首选项组中。

因此,您需要创建一个类“DataProccessor.java”并将内容放在下面。此外,我将留下一些有关如何使用它的示例。

我希望它能帮助你

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 boolean sharedPreferenceExist(String key)
    {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
        if(!prefs.contains(key)){
            return true;
        }
        else {
            return false;
        }
    }

    public static void setInt( String key, int value) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME,0);
        SharedPreferences.Editor editor = prefs.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 prefs = context.getSharedPreferences(PREFS_NAME,0);
        SharedPreferences.Editor editor = prefs.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 prefs = context.getSharedPreferences(PREFS_NAME,0);
        SharedPreferences.Editor editor = prefs.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);
    }
}
Run Code Online (Sandbox Code Playgroud)

为了使用它,我将向您展示一些关于如何保存字符串值的小示例,以防您想在活动、片段或服务中使用它。

对于活动

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

 //// To Retreive a value
 dataProccessor.getStr("email");
Run Code Online (Sandbox Code Playgroud)

对于一个片段

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

 //// To Retreive a value
 dataProccessor.getStr("email");
Run Code Online (Sandbox Code Playgroud)

对于服务

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

 //// To Retreive a value
 dataProccessor.getStr("email");
Run Code Online (Sandbox Code Playgroud)