Marco Antonio
Marco Antonio

Reputation: 339

How can I access to the SharedPreferences inside doInBackground AsyncTask?

I have a problem when I want to access to my SharedPreferences inside doInBackground. How can I do this?

Really thanks!

private class postData extends AsyncTask<String, Void, String> {
  @Override
  protected String doInBackground(String... params) {

    // I need to access here to my SharedPreferences //

  }
}

Upvotes: 1

Views: 3495

Answers (1)

Marco Antonio
Marco Antonio

Reputation: 339

The solution was easier than I thought.

private class postData extends AsyncTask<String, Void, String> {
  @Override
  protected String doInBackground(String... params) {
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext() );    
    String userName = sharedPrefs.getString("auth_username", "");
    String userPass = sharedPrefs.getString("auth_password", "");
  }
}

Upvotes: 2

Related Questions