如何根据用户角色打开Android活动? [英] How to open Android activities based on role of user?
问题描述
我正在创建一个基于在线的Android应用,其中有一个 listview ,其中显示了来自学生等不同类型用户的通知 ","老师".当我单击列表视图项时,它应检查用户的角色,用户是否注册为"老师"或"学生",并应打开活动屏幕基于其角色.我将使用意图打开不同的活动.如何从服务器检查用户的角色?
I am creating an online based Android app in which there is a listview, which shows notifications from different types of users like 'student', 'teacher'. When I click a list view item, it should check the role of the user, whether the user is registered as 'teacher' or a 'student' and it should open the activity screen based on their role. I'll use intents to open different activities. How do I check the role of the user from server?
推荐答案
好,我想提供自己的答案.我实际上使用了Shared Preferences
.它非常简单,可以全局使用我们输入的值.下面是代码:
Well, I would like to provide my own answer. I actually used Shared Preferences
. Its much simple and could globally use the values we put in it. Below is the code:
1.创建一个单独的类并根据需要命名(我在这里更喜欢SessionManager)
public class SessionManager {
// Shared Preferences
SharedPreferences sharedPrefer;
// Editor for Shared preferences
SharedPreferences.Editor editor;
// Context
Context context;
// Shared Pref mode
int PRIVATE_MODE = 0;
// Shared Pref file name
private static final String PREF_NAME = "MySession";
// SHARED PREF KEYS FOR ALL DATA
// User's UserId
public static final String KEY_USERID = "userId";
// User's categoryId
public static final String KEY_CATID = "catId";
// User's categoryType[Teacher, Student, etc.,]
public static final String KEY_CATTYPE = "categoryType";
// User's batchId[like class or level or batch]
public static final String KEY_BATCHID = "batchId";
// Constructor
public SessionManager(Context context) {
this.context = context;
sharedPrefer = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = sharedPrefer.edit();
}
/**
* Call this method on/after login to store the details in session
* */
public void createLoginSession(String userId, String catId, String catTyp, String batchId) {
// Storing userId in pref
editor.putString(KEY_USERID, userId);
// Storing catId in pref
editor.putString(KEY_CATID, catId);
// Storing catType in pref
editor.putString(KEY_CATTYPE, catTyp);
// Storing catType in pref
editor.putString(KEY_BATCHID, batchId);
// commit changes
editor.commit();
}
/**
* Call this method anywhere in the project to Get the stored session data
* */
public HashMap<String, String> getUserDetails() {
HashMap<String, String> user = new HashMap<String, String>();
user.put("userId",sharedPrefer.getString(KEY_USERID, null));
user.put("batchId",sharedPrefer.getString(KEY_BATCHID, null));
user.put("catId", sharedPrefer.getString(KEY_CATID, null));
user.put("catType", sharedPrefer.getString(KEY_CATTYPE, null));
return user;
}
}
2.在项目中的其他一些类上调用上述方法:
在会话中存储数据
SessionManager session = new SessionManager(getApplicationContext());
session.createLoginSession(userId, categoryId, categoryType, batchId);
从会话中检索数据
SessionManager session = new SessionManager(getApplicationContext());
HashMap<String, String> user = session.getUserDetails();
String userId = user.get("userId").toString();
String categoryId = user.get("catId").toString();
String categoryType = user.get("catType").toString();
String batchId= user.get("batchId").toString();
这篇关于如何根据用户角色打开Android活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!