1

当我想访问里面的SharedPreferencesdoInBackground时遇到问题。我怎样才能做到这一点?

真的谢谢!

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

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

  }
}
4

1 回答 1

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", "");
  }
}
于 2013-03-08T22:22:07.497 回答