SharedPreferences在一个复制的Android Studio项目中不起作用。

huangapple go评论106阅读模式
英文:

SharedPreferences not working in a duplicated Android Studio project

问题

我创建了一个示例应用程序,其中我使用了共享首选项,就像这样:

SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);

SharedPreferences.Editor editor = sharedPref.edit();

editor.putString("data", "这是我的数据...");

editor.apply();

然后像这样读取数据:

SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
String base_data = sharedPref.getString("data", "");

在示例应用程序中,这段代码有效,但当我将示例应用程序项目文件复制并粘贴到Android Studio中的另一个项目中时,共享首选项似乎不起作用。我尝试了一切,但它不起作用。请救救我,别让我发疯...

在“复制”的项目中,base_data 变量只返回默认值("")。

英文:

I create a sample application where I used shared preferences like this:

      SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
                    
      SharedPreferences.Editor editor = sharedPref.edit();

      editor.putString("data", "This is my data...");

                    editor.apply();

And reading the data like this:

    SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
    String base_data = sharedPref.getString("data", "");

This code worked in the sample application but when I copied and pasted the sample application project files to another project in android studio, SharedPreferences does not seem to work. I have tried everything but it does not work. Please save me from going insane...

In the "duplicate" project base_data variable just returns the default value ("").

答案1

得分: 1

public static final String PREFS_GAME = "PLAY";
public static final String GAME_SCORE = "GameScore";

//======== 保存数据的代码 ===================

SharedPreferences sp = getApplicationContext.getSharedPreferences(PREFS_GAME, Context.MODE_PRIVATE);
sp.edit().putString(GAME_SCORE, "100").commit();

//========= 获取保存的数据的代码 ==============

SharedPreferences sp = getApplicationContext.getSharedPreferences(PREFS_GAME, Context.MODE_PRIVATE);
String sc = sp.getString(GAME_SCORE, "");

英文:
public static final String PREFS_GAME ="PLAY";
public static final String GAME_SCORE= "GameScore";    

//======== Code to save data ===================

SharedPreferences sp = getApplicationContext.getSharedPreferences(PREFS_GAME ,Context.MODE_PRIVATE);
sp.edit().putString(GAME_SCORE,"100").commit();

//========= Code to get saved/ retrieve data ==============

SharedPreferences sp = getApplicationContext.getSharedPreferences(PREFS_GAME ,Context.MODE_PRIVATE);
String sc  = sp.getString(GAME_SCORE,"");

答案2

得分: 0

你需要提供一个名称来标识首选项文件

private static final String MY_PREFS_NAME = "MY_PREFS";

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("data", "这是我的数据...");
editor.apply();

从首选项获取数据:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String data = prefs.getString("data", "没有数据");

或者尝试这个类:

public class PrefManager {

    SharedPreferences pref;
    SharedPreferences.Editor editor;
    Context _context;

    // 共享首选项模式
    int PRIVATE_MODE = 0;

    // 共享首选项文件名
    private static final String PREF_NAME = "your_app_name";

    private static final String IS_FIRST_TIME_LAUNCH = "IsFirstTimeLaunch";

    public PrefManager(Context context) {
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }

    public void setBoolean(String PREF_NAME, Boolean val) {
        editor.putBoolean(PREF_NAME, val);
        editor.commit();
    }

    public void setString(String PREF_NAME, String VAL) {
        editor.putString(PREF_NAME, VAL);
        editor.commit();
    }

    public void setInt(String PREF_NAME, int VAL) {
        editor.putInt(PREF_NAME, VAL);
        editor.commit();
    }

    public boolean getBoolean(String PREF_NAME) {
        return pref.getBoolean(PREF_NAME, true);
    }

    public void remove(String PREF_NAME) {
        if (pref.contains(PREF_NAME)) {
            editor.remove(PREF_NAME);
            editor.commit();
        }
    }

    public String getString(String PREF_NAME) {
        if (pref.contains(PREF_NAME)) {
            return pref.getString(PREF_NAME, null);
        }
        return "";
    }

    public int getInt(String key) {
        return pref.getInt(key, 0);
    }
}

然后使用如下方式:

PrefManager prefManager = new PrefManager(context);
prefManager.setString("data", "我的数据");
String data = prefManager.getString("data");
英文:

You need provide a name to identify the preference file

private static final Object MY_PREFS_NAME = "MY_PREFS";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("data","This is my data...");
editor.apply();

Get data from preference:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String data = prefs.getString("data", "No data");

Or try this class

public class PrefManager {
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
// shared pref mode
int PRIVATE_MODE = 0;
// Shared preferences file name
private static final String PREF_NAME = "your_app_name";
private static final String IS_FIRST_TIME_LAUNCH = "IsFirstTimeLaunch";
public PrefManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void setBoolean(String PREF_NAME,Boolean val) {
editor.putBoolean(PREF_NAME, val);
editor.commit();
}
public void setString(String PREF_NAME,String VAL) {
editor.putString(PREF_NAME, VAL);
editor.commit();
}
public void setInt(String PREF_NAME,int VAL) {
editor.putInt(PREF_NAME, VAL);
editor.commit();
}
public boolean getBoolean(String PREF_NAME) {
return pref.getBoolean(PREF_NAME,true);
}
public void remove(String PREF_NAME){
if(pref.contains(PREF_NAME)){
editor.remove(PREF_NAME);
editor.commit();
}
}
public String getString(String PREF_NAME) {
if(pref.contains(PREF_NAME)){
return pref.getString(PREF_NAME,null);
}
return  "";
}
public int getInt(String key) {
return pref.getInt(key,0);
}

}

then

 PrefManager prefManager = new PrefManager(context);
prefManager.setString("data","mydata");
String data = prefManager.getString("data");

huangapple
  • 本文由 发表于 2020年7月21日 18:25:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63012472.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定