我正在为我的学校项目编写一个android应用程序,但我被困在这里。问题是我必须访问一个SharedPreferences
值,并需要它在AsyncTask
类。当我尝试访问它时,由于上下文,它不会让我。如何在我的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;
}
}
我的意思是访问它的值。 – user3316779
我想你不能从'AsynTask'中访问'sharedPreference',至少在'doInBackGround'中。你有没有试过从'onPreExecute'访问它? – elbaulp
只需将您需要的值传递给AsyncTask,可能在构造函数中。你甚至可以传入上下文,但是我不会在'doInBackground()'中访问它,因为它可能不是威胁安全的。 – 323go