How do I get sharedpreferences in Asynctask?
Solution 1
Override onPreExecute
or onPostExecute
method of AsyncTask
and get SharedPreferences
there:
@Override
protected void onPreExecute() {
super.onPreExecute();
//get sharedPreferences here
SharedPreferences sharedPreferences = getSharedPreferences(<SharedPreferencesName>, <Mode>);
}
Please note that getSharedPreferences
is an Activity
method. So, you need to call it through Context
if you are not using it in Activity
.
Solution 2
You can get SharedPreferences in a background thread using the PreferenceManager like this:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(<YourActivity>.this);
Solution 3
You can get SharedPreferences
in the background using an AsyncTask
.
The mistake is in this line, where you are passing in the wrong type of variable:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
In your case, the word this
refers to the AsyncTask
instance that you are calling if from.
What I do is I pass the Context
in to the task in the execute()
method. See the code below for a working example:
new AsyncTask<Context, Void, String>()
{
@Override
protected String doInBackground(Context... params)
{
Context context = params[0];
SharedPreferences prefs =
context.getSharedPreferences("name of shared preferences file",
Context.MODE_PRIVATE);
String myBackgroundPreference = prefs.getString("preference name", "default value");
return myBackgroundPreference;
}
protected void onPostExecute(String result)
{
Log.d("mobiRic", "Preference received in background: " + result);
};
}.execute(this);
In the above example, this
refers to the Activity
or Service
that I call this from.
user3316779
Updated on June 11, 2022Comments
-
user3316779 almost 2 years
I am writing an android application for my school project but i am stuck here. The problem is i have to access a
SharedPreferences
value and need it in anAsyncTask
class. When I try to access it, it wont let me because of the context. How can i reach mySharedPreferences
in myAsyncTask
?public class CheckAccess extends AsyncTask<JSONObject, Integer, Boolean>{ @Override protected Boolean doInBackground(JSONObject... params) { //Trying to get sharedpreferences here wont work. return null; } }
-
user3316779 about 10 yearsGood idea, but i am having trouble with getting it. How to continue this line in preExecute? SharedPreferences prefs = ...
-
user3316779 about 10 yearsI think the problem is that i don't call it through Context since i am not using it in an activity. How can i do it?
-
Alejandro Alcalde about 10 yearsSimply pass the context to the
AsynTask
constructor. -
Alejandro Alcalde about 10 yearsThen you can not get the preferences I think. You need the Activity
-
Green goblin about 10 yearsYou must be using the AsynTask class from an activity. Before calling it, call the constructor of class where Asynctask is residing. In the constructor, pass the context.
-
user3316779 about 10 yearsI am using it like this: jsonObject = new GetData().execute(cardId).get(); What changes do i need?
-
Green goblin about 10 yearsDefine a constructor of
CheckAccess
to acceptContext
. In the constructor definition, initialize Context. Pass theContext
while creating object. -
Richard Le Mesurier over 9 yearsI'm afraid this answer is wrong. See tlahoda's answer for a summary or my answer for a more detailed explanation.
-
Green goblin over 9 years@RichardLeMesurier: Thanks for correcting me. I totally missed it. I have corrected my answer