2

I am migrating iOS app to android and, thinking about iOS, I set a default plist file with strings and keys that are then modified using NSuserDefaults. I read some posts and docs and not really clear about how to do it properly in Android. I need a preferences file with keys and strings that I could read and write. Thank you.

ucMedia
  • 4,105
  • 4
  • 38
  • 46
Jaume
  • 3,672
  • 19
  • 60
  • 119

2 Answers2

22
SharedPreferences settings;
settings = getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);

//get the sharepref
int id = settings.getInt("ID", 0);

//set the sharedpref
Editor editor = settings.edit();
editor.putInt("ID", "1");
editor.commit();
A--C
  • 36,351
  • 10
  • 106
  • 92
Bhavesh Vadalia
  • 369
  • 2
  • 7
0

Read Settings

public static String readSetting(Context context, String key, String defaul){
        SharedPreferences preferences = context.getSharedPreferences("mySettings",MODE_PRIVATE);
        return preferences.getString(key,defaul);
    }

save settings-

@SuppressLint("ApplySharedPref")
public static void saveSetting(Context context, String key, String valu){
    SharedPreferences preferences = context.getSharedPreferences("mySettings",MODE_PRIVATE);
    SharedPreferences.Editor edit= preferences.edit();
    edit.putString(key,valu);
    edit.commit();

}
MbPCM
  • 457
  • 5
  • 12