android context and sharedpreferences

偶尔善良 提交于 2019-12-25 14:01:18

问题


I am trying to get context to run async sharedpreferences. Tried to get context with:

public class MainActivity2 extends Activity implements OnClickListener {
    public MainActivity2(final Context context) {
        this.context = context;
    }
    private Context context;

    //....rest of class.....
}

But the app crashes when that it is included. But need something like that to get to sharedpreferences:

class CreateUser extends AsyncTask<String, String, String> {
    // .....rest of ....
    @Override
    protected String doInBackground(String... args) {
        SharedPreferences prefs =   android.preference.PreferenceManager.getDefaultSharedPreferences(context);
        String myIntegerValue = prefs.getString("ok", "f");
        android.util.Log.d("your_tag", "myint: " + myIntegerValue);
    }
    //rest of.....
}

trying to get sharedpreferences like this does not work:

SharedPreferences prefs = android.preference.PreferenceManager.getDefaultSharedPreferences(getactivity());

trying to get sharedpreferences like this does not work:

SharedPreferences prefs = android.preference.PreferenceManager.getDefaultSharedPreferences(this);

getDefaultSharedPreferences cannot be applied to MainActivity2.CreateUser when using this


回答1:


But the app crashes when that it is included. But need something like that to get to sharedpreferences:

Activity extends ContextWrapper. You don't need (and you can't have it) the constructor that takes a context as parameter. Your context in the activity is the keyword this. If you need it in a Fragment you can use getActivity() to retrieve the context of the activity that is hosting the Fragment.

Edit:

in your case you have to possibilities. You add a constructor that takes a Context to the AsyncTask, or you read the values in the Activity and pass it to the AsyncTask. I would recommend you the second approach




回答2:


while using shared preferences in asynctask you should in onPreExecute method get all data from sharedprefs to your local variable, use them inside doInBackground and if needed update those values, do it in onPostExecute.

You don't need to explicitly declare context anywhere if u are using asynctask in same class. you can just write Classname.this.

Better way to implement it is initialize your sharedprefs in oncreate as u can use it anywhere.

In case if you are writing aynctask in different class then you can write a function which will accept context and will call asynctask to get executed.




回答3:


Try this:

SharedPreferences prefs = android.preference.PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());

or

SharedPreferences prefs = android.preference.PreferenceManager.getDefaultSharedPreferences(getApplicationContext());



回答4:


just use ur Activity as Context MainActivity.this should do the trick



来源:https://stackoverflow.com/questions/29790930/android-context-and-sharedpreferences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!