-3

I have a class with all common functions and there i want to get some of sharedpreferences value. below code getSharedPreference() is giving error.

Here's my code:

public class CommonFunctions {

Context mContext;
SharedPreferences sharedPreferences;

    public CommonFunctions(Context context) {
       this.mContext = context;
       sharedPreferences = getSharedPreference("MyPREFERENCES", MODE_PRIVATE);
    }

}

Is there any way to get value in java class?

manjari
  • 183
  • 1
  • 14
  • you have to use context.getSharedPreference("MyPREFERENCES", context.MODE_PRIVATE); – Bharat Aug 04 '17 at 06:14
  • You can't directly use context related functions outside of activity – Bharat Aug 04 '17 at 06:15
  • check this answer https://stackoverflow.com/a/7497079/3983054 – King of Masses Aug 04 '17 at 06:15
  • 1
    Possible duplicate of [Get Android shared preferences value in activity/normal class](https://stackoverflow.com/questions/7496840/get-android-shared-preferences-value-in-activity-normal-class) – Bharat Aug 04 '17 at 06:16

1 Answers1

1

Use below code;

 sharedPreferences = context.getSharedPreference("MyPREFERENCES", MODE_PRIVATE);
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57