Android 将用户发送到简历上的主要活动
在我的应用程序中,我将登录活动作为主启动器。当用户第一次打开应用程序时,用户需要输入凭证才能转到主活动。在用户登录时,我已经在首选项中保存了一些主要活动需要的信息。 现在我想要的是,当任何一个用户再次打开应用程序时,假设用户没有注销,用户应该在首选项中保存的信息的帮助下直接发送到主活动 下面是登录活动中创建新会话的代码Android 将用户发送到简历上的主要活动,android,xamarin.android,Android,Xamarin.android,在我的应用程序中,我将登录活动作为主启动器。当用户第一次打开应用程序时,用户需要输入凭证才能转到主活动。在用户登录时,我已经在首选项中保存了一些主要活动需要的信息。 现在我想要的是,当任何一个用户再次打开应用程序时,假设用户没有注销,用户应该在首选项中保存的信息的帮助下直接发送到主活动 下面是登录活动中创建新会话的代码 if (res.carExists != true) { MyMessageBox.SetAlertBox("Opps!
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);
}
}
}
有人能给我一些提示吗?我如何利用共享首选项中保存的信息将用户引导到主要活动。当用户登录时,您正在共享首选项中保存一些值,下次应用打开时,请在登录活动中检查该共享首选项值(在执行登录操作之前)。如果为真(例如),则启动主活动(完成登录活动,以便用户在按下“后退”按钮后不应返回登录活动),否则继续登录活动。当用户注销时,再次将共享首选项值替换为false,以便下次应用程序打开时,控件应首先进入登录活动。最好在执行登录操作之前先执行onCreate。。。此共享首选项值检查应该是登录活动中的第一行代码。。。