Java 如何在 Asynctask 中获取共享首选项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23596903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get sharedpreferences in Asynctask?
提问by user3316779
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 an AsyncTask
class. When I try to access it, it wont let me because of the context. How can i reach my SharedPreferences
in my AsyncTask
?
我正在为我的学校项目编写一个 android 应用程序,但我被困在这里。问题是我必须访问一个SharedPreferences
值并在AsyncTask
类中需要它。当我尝试访问它时,它不会让我因为上下文。我怎样才能达到我SharedPreferences
的AsyncTask
?
public class CheckAccess extends AsyncTask<JSONObject, Integer, Boolean>{
@Override
protected Boolean doInBackground(JSONObject... params) {
//Trying to get sharedpreferences here wont work.
return null;
}
}
采纳答案by Green goblin
Override onPreExecute
or onPostExecute
method of AsyncTask
and get SharedPreferences
there:
覆盖onPreExecute
或onPostExecute
方法AsyncTask
并到达SharedPreferences
那里:
@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
.
请注意,这getSharedPreferences
是一种Activity
方法。因此,Context
如果您不在Activity
.
回答by tlahoda
You can get SharedPreferences in a background thread using the PreferenceManager like this:
您可以使用 PreferenceManager 在后台线程中获取 SharedPreferences,如下所示:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(<YourActivity>.this);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(<YourActivity>.this);
回答by Richard Le Mesurier
You can get SharedPreferences
in the background using an AsyncTask
.
您可以SharedPreferences
使用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.
在您的情况下,该词this
指的AsyncTask
是您正在调用的实例。
What I do is I pass the Context
in to the task in the execute()
method. See the code below for a working example:
我所做的是将Context
in传递给方法中的任务execute()
。有关工作示例,请参阅下面的代码:
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.
在上面的例子中,this
指的是我称之为的Activity
或Service
。