3
我有一个Android应用程序登录,注销功能。登录表单包含用户名和密码以及登录按钮。我想在用户选中“记住我”复选框时保存用户名和密码。保存登录信息(首选项)android
我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){
}
}
我可以在同一个Java文件本身使用上面的代码:该首选项可以随时使用您为这些首选项
设置键访问信息,就像在你onCreate方法进行访问? – user768990
是的,你可以。您的应用中的所有活动都可以访问相同的SharedPreferences。 –
非常感谢你,我现在检查一下 – user768990