java保存登录信息_java – 保存登录详细信息(首选项)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 nameValuePairs = new ArrayList(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){
}
}
解決方法:
您可以使用應用共享偏好設置.可以使用為這些prefs設置的密鑰隨時訪問prefs
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
來源:https://www.icode9.com/content-1-401801.html
總結
以上是生活随笔為你收集整理的java保存登录信息_java – 保存登录详细信息(首选项)android的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql按select导出_mysql
- 下一篇: kali2020识别不到网卡_WIN10