我允许注册用户访问该应用。用户每次打开应用程序时都必须登录。所以我想知道如何单独登录应用程序,如facebook或whatsapp?
答案 0 :(得分:1)
您可以使用 共享偏好设置 。
Android提供了许多存储应用程序数据的方法。其中一种方法称为Shared Preferences。共享首选项允许您以key,value pair
。
您可以使用共享偏好设置获取值 getSharedPreferences()方法。
您还需要一个编辑器来编辑和保存共享中的更改 偏好。
演示
答案 1 :(得分: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
}
答案 2 :(得分: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创建一个类,并在您想要作为登录页面的活动中调用它。供参考,请参阅this教程