私のアプリケーションでは、メインランチャーとしてログインアクティビティを作成しました。ユーザーが初めてアプリを開くとき、ユーザーはメインアクティビティに移動するためにクレデンシャルを入力する必要があります。ユーザーがログインしたときに、メインアクティビティに必要な情報を[設定]に保存しました。今私が欲しいのは、ユーザーがアプリを再び開くときはいつでも、ユーザーがログアウトしていないと仮定して、ユーザーは設定に保存された情報の助けを借りてメインアクティビティに直接送信される必要があります。
以下は、新しいセッションを作成するログインアクティビティのコードです。
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);
}
以下は、ログイン時にユーザー情報を保存するSessionManagerのコードです。
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);
}
}
}
誰かが共有設定に保存された情報の助けを借りてユーザーをメインアクティビティに誘導する方法についていくつかのヒントを教えてもらえますか?