如何访问doInBackground AsyncTask中的SharedPreferences?

时间:2013-03-08 22:10:00

标签: android android-asynctask

当我想要访问doInBackground内的 SharedPreferences 时,我遇到了问题。我怎么能这样做?

非常感谢!

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

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

  }
}

1 个答案:

答案 0 :(得分:2)

解决方案比我想象的要容易。

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", "");
  }
}