I am developing an application for android and I am new to it, For getting the value from SharedPreferences, I copied and pasted this piece of code below :
SharedPreferences prefs =  PreferenceManager.getDefaultSharedPreferences(getActivity());
                String myValue = prefs.getString(getString(R.string.my_key),
                        getString(R.string.my_default_key));
Obviously this is done inside an Activity, but for storing the key value into SharedPreferences I have some issues it should be done inside a Class which is not an Activity therefore the above code is not working. 
Question: 
I need to know if there is any way that I can use to store and the key value pair inside a class, so that I could retrieve the key value in anActivity with the code above or do I have to modify my code and changed it something like the answered question in this link?
How to use SharedPreferences in Android to store, fetch and edit values
 
     
     
     
    