java - 保存登录详细信息(首选项)android

标签 java android preferences remember-me

我有一个具有登录、注销功能的安卓应用程序。登录表单包含用户名和密码以及登录按钮。当用户选中“记住我”复选框时,我想保存用户名和密码。

我的project.java文件如下所示:

public class project extends Activity {

private static final int IO_BUFFER_SIZE = 4 * 1024;

/** Called when the activity is first created. */
public int user_id,current_user_id;

public String access_token,username,current_username;
public boolean user_logged_in=false;
public ProgressDialog progdialog;



@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    progdialog = new ProgressDialog(this);
    progdialog.setMessage("loading...");

    initLogin();
}

public void initLogin(){
    setContentView(R.layout.login);


    Button button = (Button) findViewById(R.id.login_login);
    button.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            login();
        }
    });
}
public void login(){
    try{
        String login_username=(((EditText) findViewById(R.id.login_username)).getText()).toString();
        String login_password=(((EditText) findViewById(R.id.login_password)).getText()).toString();
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username",login_username));
        nameValuePairs.add(new BasicNameValuePair("password",login_password));
        String content=postUrlContent(api_url+"landing/authenticate",nameValuePairs);
        JSONObject jObject = new JSONObject(content);
        JSONObject respObject = jObject.getJSONObject("response");
        Boolean success=respObject.getBoolean("success");
        String message=respObject.getString("message");
        Boolean logged_in=respObject.getBoolean("logged_in");
        if(logged_in){
            user_id=respObject.getInt("user_id");
            access_token=respObject.getString("access_token");
            username=respObject.getString("username");
            current_username=username;
            current_user_id=user_id;
            user_logged_in=true;
            //initFeeds(username);
            test(0);

        }else{
            createDialog("Error",message);
        }
    }catch(Exception e){

    }
}

最佳答案

您可以使用您的应用共享首选项。可以使用您为这些首选项设置的 key 随时访问首选项

 static final String KEY_USERNAME = "username";
 static final String KEY_PASSWORD = "password";

 if (rememberMe) { //save username and pw to prefs
      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
      Editor ed = prefs.edit();
      ed.putString(KEY_USERNAME, theUsername);
      ed.putString(KEY_PASSWORD, thePW);
      ed.commit();
 }

要访问信息,例如在您的 onCreate 方法中:

 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
 String username = prefs.getString(KEY_USERNAME, "Default Value if not found");
 String password = prefs.getString(KEY_PASSWORD, ""); //return nothing if no pass saved

关于java - 保存登录详细信息(首选项)android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7549802/

相关文章:

android - registerOnSharedPreferenceChangeListener 不起作用

Eclipse Luna 无法保存首选项

java - java写入文件时遇到问题

preferences - Liferay:如何将用户信息保存到portlet?

java - XSL 转换模板仅返回最后一个属性值

java - 定义的 TextView 上出现 NullPointerException

java - 找出其他两个点之间点 x 和 y 的百分比

c# - 从 Assets 中读取文本文件的问题(xamarin)

Java 套接字 - java.net.ConnectException : Connection refused: connect

java - 想要将json数据插入Sqlite数据库