Search code examples
javaandroidandroid-studiosharedpreferencesworker

How can I get my sharedpreferences instance insise a worker


I have a worker and want to access my shared preferences. In my worker class I can't do

this.getSharedPreferences

and

getSharedPreferences()

isn't static so I can't do

MainActivity.getSharedPreferences

Does anyone have an idea how to do this?


Solution

  • You can simply call getApplicationContext() method directly from the Worker class to get the Context of the entire application, which should be reasonable in this use case.