我允許註冊用戶訪問該應用程序。用戶每次打開應用程序時都必須登錄。所以我想知道如何一次性登錄應用程序,如Facebook或WhatsApp?如何在android應用程序中登錄一次?
1
A
回答
1
您可以使用共享偏好。
Android提供了許多存儲應用程序數據的方法。其中一種方式稱爲Shared Preferences。共享首選項允許您以key,value pair
的形式保存和檢索數據。
- 您可以從Shared 首選項中保存和檢索關鍵值對數據。 SharedPreferences值將在用戶 會話中持續存在。
- 即使用戶關閉了 應用程序,共享首選項中的數據仍將保留。
您可以使用 getSharedPreferences()方法從共享首選項中獲取值。
您還需要編輯器來編輯和保存共享 首選項中的更改。
- 使用SharedPreferences存儲數據:布爾值,浮點數,整數,長整數, 和字符串。
演示
0
您應該使用SharedPreferences來存儲成功的登錄操作。
保存狀態共享偏好
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("username", "yourUserName");
editor.putBoolean("isLoggedIn", true);
editor.commit();
從SharedPreference
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
boolean isLoggedIn = prefs.getBoolean("isLoggedIn", false);
if (isLoggedIn) {
// Don't display login activity
} else {
// Display Login Activity
}
檢索狀態
0
public class SessionManager {
// Email address (make variable public to access from outside)
public static final String KEY_api = "apikey";
private static final String KEY_NAME = "name";
// Sharedpref file name
private static final String PREF_NAME = "AndroidHivePref";
private static final String push_ = "AndroidHivePref";
// All Shared Preferences Keys
private static final String IS_LOGIN = "IsLoggedIn";
// Shared Preferences
SharedPreferences pref;
// Editor for Shared preferences
SharedPreferences.Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Constructor
public SessionManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void createLoginSession(String name, String apikey) {
// Storing login value as TRUE
editor.putBoolean(IS_LOGIN, true);
// Storing name in pref
editor.putString(KEY_NAME, name);
// Storing email in pref
editor.putString(KEY_api, apikey);
// commit changes
editor.commit();
}
public int getMerchentPushCount() {
return pref.getInt("MerchentPushCount", 0);
}
public void setMerchentPushCount(Integer count) {
editor.putInt("MerchentPushCount", count);
editor.commit();
}
/**
* Check login method wil check user login status
* If false it will redirect user to login page
* Else won't do anything
*/
public void checkLogin() {
// Check login status
if (!this.isLoggedIn()) {
// user is not logged in redirect him to Login Activity
Intent i = new Intent(_context, LoginActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
}
/**
* Get stored session data
*/
public HashMap<String, String> getUserDetails() {
HashMap<String, String> user = new HashMap<String, String>();
user.put(KEY_NAME, pref.getString(KEY_NAME, null));
// user api key
user.put(KEY_api, pref.getString(KEY_api, null));
// return user
return user;
}
/**
* Clear session details
*/
public void logoutUser() {
// Clearing all data from Shared Preferences
editor.clear();
editor.commit();
// After logout redirect user to Loing Activity
Intent i = new Intent(_context, LoginActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
// Staring Login Activity
_context.startActivity(i);
}
/**
* Quick check for login
* *
*/
// Get Login State
public boolean isLoggedIn() {
return pref.getBoolean(IS_LOGIN, false);
}
}
創建剪切prefrence一類,並調用它在烏爾活動其中U想使作爲登陸頁面。參考以下this教程
相關問題
- 1. 如何在Android應用程序中登錄一次?
- 2. 如何在Android應用程序中僅顯示一次登錄屏幕?
- 3. 在Android中登錄應用程序
- 4. Bypassing在Android應用程序中登錄
- 5. Android應用程序登錄
- 6. Android應用程序登錄
- 7. Android登錄應用程序
- 8. Android登錄應用程序
- 9. 如何在Android應用程序中使用Facebook登錄?
- 10. 如何區分第一次谷歌登錄和連續谷歌登錄在Android應用程序?
- 11. 在Android應用程序Facebook SDK登錄
- 12. Android應用程序登錄,在後臺
- 13. 如何在android中爲應用程序創建'登錄'代碼?
- 14. 如何在Android應用程序中實現Facebook登錄?
- 15. 我們如何在Android應用程序中單點登錄Facebook?
- 16. 如何在Android應用程序中實現登錄
- 17. 一個在應用程序中登錄的web應用程序
- 18. 如何在每次出現應用程序時要求登錄
- 19. 如何讓用戶在Android上登錄應用程序?
- 20. 如何讓單個用戶登錄應用程序在Android的
- 21. Facebook登錄在Android中第一次後登錄不起作用
- 22. Android使用Facebook登錄應用程序
- 23. Android - 使用jarsigner登錄應用程序
- 24. 使用Android應用程序登錄webservices
- 25. Node.js Express應用程序只在登錄時插入app.local.setting一次
- 26. 一次性通過Facebook在iOS應用程序登錄
- 27. 如何添加在線登錄android應用程序
- 28. 如何在運行Android應用程序時自動登錄
- 29. Android:如何將Google加入登錄到Android應用程序?
- 30. 谷歌登錄每次關閉應用程序後出現android
使用剪切偏好爲此目的。 –
是的共享偏好 –
我想這實際上取決於你如何實現你的後端,你可以給我們更多的細節,如何以及你用什麼來實現登錄? – TPWang