在我的应用程序中,我将登录活动作为主启动器。当用户第一次打开应用程序时,用户需要输入凭据才能进入主要活动。在用户登录时,我已经在首选项中保存了主要活动所需的一些信息。现在我想要的是,当用户再次打开应用程序时,假设用户尚未注销,应借助保存在首选项中的信息将用户直接发送到主要活动。
下面是登录活动中创建新会话的代码。
if (res.carExists != true)
{
MyMessageBox.SetAlertBox("Opps!!!!!!!!", "This Car Number Was Wrong!!!!", "OK", m_context);
}
else
{
string carType = res.carType;
string seatNum = res.numOfSeats.ToString();
// MainActivity act = new MainActivity( result.driverId );
session = new SessionManger(m_context);
session.createLoginSession(result.driverId.ToString());
var mact = new Intent(m_context, typeof(MainActivity));
mact.PutExtra("driverID", result.driverId.ToString());
MyMessageBox.SetAlertBox("Comfirm!", "Your car is a: " + carType + " with " + seatNum + " seats??", "Yes", "No", mact, m_context);
}
下面是会话管理器的代码,它将用户信息保存在登录中。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Preferences;
namespace NorthStar.Driver
{
public class SessionManger
{
ISharedPreferencesEditor editor;
ISharedPreferences pref;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Sharedpref file name
private static readonly String PREF_NAME = "AndroidHivePref";
// All Shared Preferences Keys
private static readonly String IS_LOGIN = "IsLoggedIn";
// User name (make variable public to access from outside)
public static readonly String KEY_NAME = "driver";
// Constructor
public SessionManger(Context context)
{
this._context = context;
pref = _context.GetSharedPreferences(PREF_NAME, Android.Content.FileCreationMode.Private);
editor = pref.Edit();
}
public void createLoginSession(String driverID)
{
// Storing login value as TRUE
editor.PutBoolean(IS_LOGIN, true);
// Storing name in pref
editor.PutString(KEY_NAME, driverID);
editor.Commit();
}
public void checkLogin()
{
if (!this.isLoggedIn())
{
Intent i = new Intent(_context, typeof(Activity1));
i.AddFlags(ActivityFlags.ClearTop);
i.SetFlags(ActivityFlags.NewTask);
_context.StartActivity(i);
}
}
public string getDriver()
{
return pref.GetString(KEY_NAME, "");
}
public Boolean isLoggedIn()
{
return pref.GetBoolean(IS_LOGIN, false);
}
}
}
有人可以给我一些提示,我如何在共享首选项中保存信息的帮助下将用户引导到主要活动。