How do I get sharedpreferences in Asynctask?

前端 未结 3 1996
心在旅途
心在旅途 2020-12-19 04:40

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

3条回答
  •  情歌与酒
    2020-12-19 05:14

    Override onPreExecute or onPostExecute method of AsyncTask and get SharedPreferences there:

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //get sharedPreferences here
        SharedPreferences sharedPreferences = getSharedPreferences(, );
    }
    

    Please note that getSharedPreferences is an Activity method. So, you need to call it through Context if you are not using it in Activity.

提交回复
热议问题