如何让用户始终保持登录 "Connected with Server"
How to keep user Logged-In always "Connected with Server"
我正在开发一个应用程序,我希望用户保持登录状态意味着在成功登录后始终连接到服务器(就像 facebook 应用程序一样)。我尝试 google 这个,但没有找到任何正确的逻辑。许多网站建议使用 SharedPreference
但保留用户的登录凭据。在 SheredPreference 上不是个好主意,也没有提供任何答案来保持与服务器的连接。我有点坚持这个想法。我只需要逻辑来实现这个。欢迎任何建议和示例代码。
我是android菜鸟。
你的问题好像不太清楚。
1) 始终连接到服务器是什么意思?
2) 如果用户连接到服务器,你需要做什么样的事情?
如果你想让用户在你的应用中一直登录,我建议你使用SharedPreferences
,不需要在SharedPreferences
中存储用户凭据,你可以存储userId,电子邮件地址和此类详细信息。SharePreferences
如果你想要一些基于时间的信息,比如需要每天或每小时更新数据,你可以在给定时间使用 AlarmManager
调用 API。AlarmManager
您仍然需要一些信息来通知用户新的 change/update,您可以使用推送通知。GCM and FCM
注:
Firebase Cloud Messaging (FCM) is the new version of GCM.
首先,理想情况下,您应该在用户登录时生成一个令牌(facebook 应用程序也使用 oauth 令牌),然后将其存储在您的设备和服务器上。将电子邮件地址或任何其他此类用户信息存储在 phone.
上也不是一个好主意
在服务器端创建和维护会话。接下来,让应用程序在设定的时间间隔后连接到母舰,即服务器,并发送一条 "I am alive" 消息。如果您在服务器端收到消息,则会增加会话时间。
这样,用户将永远保持登录状态,但前提是用户保持活动状态。
服务器和应用程序在发送或接收数据之前都必须先检查会话和令牌。这可确保用户获得授权,应用程序未被强制关闭,并且用户仍保持连接状态。如果您想要更多,请进一步询问。
首先,我不明白术语 stay connected to server
和 stay logged in
在您的案例中的用法。但据我所知,我会回答这个问题。
要保持登录状态,而不是每次都要求提供凭据,您应该从服务器获取一个唯一令牌并将其与其他登录详细信息(密码除外)一起存储在SharedPreferences 或在某些数据库中。每当用户打开应用程序时,使用收到的令牌作为身份验证参数(您也可以参考誓言方法)。这将消除密码泄露的可能性,令牌将像会话一样特定于设备。
保持与服务器的连接,接收即时通知,发送和接收消息?应用程序打开时,使用套接字,就是这样,应用程序关闭时,您可以使用FCM。
试试这个对我有用..
sessionManager.java
package com.example.sachin.splashlogin;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import java.util.HashMap;
public class SessionManager {
SharedPreferences pref;
// Editor for Shared preferences
Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Sharedpref file name
private static final String PREF_NAME = "SocialPref";
// All Shared Preferences Keys
private static final String IS_LOGIN = "IsLoggedIn";
// User name (make variable public to access from outside)
public static final String KEY_NAME = "email";
// Email address (make variable public to access from outside)
public static final String KEY_ID = "user_id";
// Constructor
public SessionManager(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
/**
* Create login session
* */
public void createLoginSession(String email, String userid){
// Storing login value as TRUE
editor.putBoolean(IS_LOGIN, true);
// Storing name in pref
editor.putString(KEY_NAME, email);
// Storing email in pref
editor.putString(KEY_ID, userid);
// commit changes
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, com.example.sachin.splashlogin.Login.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 name
user.put(KEY_NAME, pref.getString(KEY_NAME, null));
// user email id
user.put(KEY_ID, pref.getString(KEY_ID, null));
// return user
return user;
}
/**
* Clear session details
* */
public void logoutUser(){
// Clearing all data from Shared Preferences
editor.clear();
editor.commit();
editor.putBoolean(IS_LOGIN, false);
// After logout redirect user to Loing Activity
Intent i = new Intent(_context, Login.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);
}
/**
* Quick check for login
* **/
// Get Login State
public boolean isLoggedIn(){
return pref.getBoolean(IS_LOGIN, false);
}
}
在每个新屏幕中,您只需粘贴此代码..
SessionManager session;
将此代码粘贴到 onCreate()
session = new SessionManager(getApplicationContext());
HashMap<String, String> user = session.getUserDetails();
struid = user.get(SessionManager.KEY_NAME);
将用户的凭据存储在设备上并不是一种好的设计方式。您可以存储 Hash
密码,这也被拒绝作为良好的应用程序设计技术。根据 facebook 和 google 这些科技巨头使用 Authentication
令牌登录-注销。一旦用户登录服务器,就会为特定用户生成令牌,然后将其存储在您的设备和服务器上。下次用户访问应用程序时,已发出检查令牌是否有效的请求,如果有效 - 则授予访问权限。
这个流程的基本设计
教程:
我正在开发一个应用程序,我希望用户保持登录状态意味着在成功登录后始终连接到服务器(就像 facebook 应用程序一样)。我尝试 google 这个,但没有找到任何正确的逻辑。许多网站建议使用 SharedPreference
但保留用户的登录凭据。在 SheredPreference 上不是个好主意,也没有提供任何答案来保持与服务器的连接。我有点坚持这个想法。我只需要逻辑来实现这个。欢迎任何建议和示例代码。
我是android菜鸟。
你的问题好像不太清楚。
1) 始终连接到服务器是什么意思?
2) 如果用户连接到服务器,你需要做什么样的事情?
如果你想让用户在你的应用中一直登录,我建议你使用SharedPreferences
,不需要在SharedPreferences
中存储用户凭据,你可以存储userId,电子邮件地址和此类详细信息。SharePreferences
如果你想要一些基于时间的信息,比如需要每天或每小时更新数据,你可以在给定时间使用 AlarmManager
调用 API。AlarmManager
您仍然需要一些信息来通知用户新的 change/update,您可以使用推送通知。GCM and FCM
注:
Firebase Cloud Messaging (FCM) is the new version of GCM.
首先,理想情况下,您应该在用户登录时生成一个令牌(facebook 应用程序也使用 oauth 令牌),然后将其存储在您的设备和服务器上。将电子邮件地址或任何其他此类用户信息存储在 phone.
上也不是一个好主意在服务器端创建和维护会话。接下来,让应用程序在设定的时间间隔后连接到母舰,即服务器,并发送一条 "I am alive" 消息。如果您在服务器端收到消息,则会增加会话时间。
这样,用户将永远保持登录状态,但前提是用户保持活动状态。
服务器和应用程序在发送或接收数据之前都必须先检查会话和令牌。这可确保用户获得授权,应用程序未被强制关闭,并且用户仍保持连接状态。如果您想要更多,请进一步询问。
首先,我不明白术语 stay connected to server
和 stay logged in
在您的案例中的用法。但据我所知,我会回答这个问题。
要保持登录状态,而不是每次都要求提供凭据,您应该从服务器获取一个唯一令牌并将其与其他登录详细信息(密码除外)一起存储在SharedPreferences 或在某些数据库中。每当用户打开应用程序时,使用收到的令牌作为身份验证参数(您也可以参考誓言方法)。这将消除密码泄露的可能性,令牌将像会话一样特定于设备。
保持与服务器的连接,接收即时通知,发送和接收消息?应用程序打开时,使用套接字,就是这样,应用程序关闭时,您可以使用FCM。
试试这个对我有用..
sessionManager.java
package com.example.sachin.splashlogin;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import java.util.HashMap;
public class SessionManager {
SharedPreferences pref;
// Editor for Shared preferences
Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Sharedpref file name
private static final String PREF_NAME = "SocialPref";
// All Shared Preferences Keys
private static final String IS_LOGIN = "IsLoggedIn";
// User name (make variable public to access from outside)
public static final String KEY_NAME = "email";
// Email address (make variable public to access from outside)
public static final String KEY_ID = "user_id";
// Constructor
public SessionManager(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
/**
* Create login session
* */
public void createLoginSession(String email, String userid){
// Storing login value as TRUE
editor.putBoolean(IS_LOGIN, true);
// Storing name in pref
editor.putString(KEY_NAME, email);
// Storing email in pref
editor.putString(KEY_ID, userid);
// commit changes
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, com.example.sachin.splashlogin.Login.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 name
user.put(KEY_NAME, pref.getString(KEY_NAME, null));
// user email id
user.put(KEY_ID, pref.getString(KEY_ID, null));
// return user
return user;
}
/**
* Clear session details
* */
public void logoutUser(){
// Clearing all data from Shared Preferences
editor.clear();
editor.commit();
editor.putBoolean(IS_LOGIN, false);
// After logout redirect user to Loing Activity
Intent i = new Intent(_context, Login.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);
}
/**
* Quick check for login
* **/
// Get Login State
public boolean isLoggedIn(){
return pref.getBoolean(IS_LOGIN, false);
}
}
在每个新屏幕中,您只需粘贴此代码..
SessionManager session;
将此代码粘贴到 onCreate()
session = new SessionManager(getApplicationContext());
HashMap<String, String> user = session.getUserDetails();
struid = user.get(SessionManager.KEY_NAME);
将用户的凭据存储在设备上并不是一种好的设计方式。您可以存储 Hash
密码,这也被拒绝作为良好的应用程序设计技术。根据 facebook 和 google 这些科技巨头使用 Authentication
令牌登录-注销。一旦用户登录服务器,就会为特定用户生成令牌,然后将其存储在您的设备和服务器上。下次用户访问应用程序时,已发出检查令牌是否有效的请求,如果有效 - 则授予访问权限。
这个流程的基本设计
教程: