|
|
|
Android - login ได้ค่า ID แล้ว แต่อยากให้ทุกคลาส Activity เรียกใช้ได้ เขียนโค้ดยังไง |
|
|
|
|
|
|
|
Shared Preferences ลองดูครับ
|
|
|
|
|
Date :
2013-10-14 06:36:35 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
มีตัวอย่างวิธีใช้ไหมค่ะ
|
|
|
|
|
Date :
2013-10-14 23:31:38 |
By :
moo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตัวอย่าง UserHelper.java
Code (Java)
public class UserHelper {
Context context;
SharedPreferences sharedPerfs;
Editor editor;
// Prefs Keys
static String perfsName = "UserHelper";
static int perfsMode = 0;
public UserHelper(Context context) {
this.context = context;
this.sharedPerfs = this.context.getSharedPreferences(perfsName, perfsMode);
this.editor = sharedPerfs.edit();
}
public void createSession(String key) {
editor.putBoolean("isLogin", true);
editor.putString("userID", key);
editor.commit();
}
public void deleteSession() {
editor.clear();
editor.commit();
}
public boolean isLogin() {
return sharedPerfs.getBoolean("isLogin", false);
}
public String getUserID() {
return sharedPerfs.getString("userID", null);
}
}
วิธีใช้
Code (Java)
UserHelper usrHelper = new UserHelper(this);
// Create Session
usrHelper.createSession(ID);
// Delete Session
usrHelper.deleteSession();
// Check Login
if (usrHelper.isLogin()) {
} else {
}
// Get UserID
usrHelper.getUserID()
|
|
|
|
|
Date :
2013-10-25 11:12:11 |
By :
C2oWisComing |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|