我的应用程序有三种登录类型 1.使用Web服务正常注册和登录 2.Facebook整合 3.谷歌集成 我需要知道的是如何管理三种类型登录的 session 。我使用共享首选项进行正常登录,我能够管理多个用户的 session ,但需要帮助管理这三种类型的 session
最佳答案
您可以使用 SharedPreferences 和 Gson,如下所示:
public class Prefs {
public static void putPref(String key, String value, Context context){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, value);
editor.commit();
}
public static String getPref(String key, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(key, null);
}
}
将其添加到您的 build.gradle 中:
compile 'com.google.code.gson:gson:1.7.2'
在登录( Activity 或 fragment )中:
Gson gson = new Gson();
User user = ...; //User Object specify your login type with enum or ...
String json = gson.toJson(user);
存储您的用户后
Prefs.putPref("user",json,your context);
获取您的用户
String json = = Prefs.getPref("user",your context);
User user = null;
if(json != null)
user = gson.fromJson(json,User.class);
关于java - 如果应用程序有多个登录(例如 Facebook、Google 和 Web 服务登录),如何在 Android 中管理 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47843051/