简体   繁体   中英

Android get shared preferences from different files

in my android application i've got 4 differents shared preferences files, each one with his preferences.

To access and modify them i use a setting activity.

I've got some preferences which are not in this files but are in DefaultPreferences.

How can i read each shared preference from this files one by one?

I found this code but i can't get it works.

  SharedPreferences prefs_clima = getSharedPreferences("prefs_clima",MODE_PRIVATE);
            Map<String,?> keys = prefs_clima.getAll();
            for(Map.Entry<String,?> entry : keys.entrySet()){
                Log.d("map values",entry.getKey() + ": " +
                        entry.getValue().toString());
            }

I always get zero preferences when reading.

follow this method and try to retrive the values

// retrive sharedpreference

    SharedPreferences sharedPref = getSharedPreferences("prefs_clima", Context.MODE_PRIVATE); 

//get values and assign it to variables
                String yourData = sharedPref.getString("keyName", "defaultValue");
//to get retrive int value
                int intValue = sharedPref.getInt("KeyName", 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM