I've been working on this for a couple of days. I have an app that needs to time out after a predefined number of minutes (have this saved in the device settings). I had tried the xamarin forms onsleep/onresume but they weren't firing at all. So I went device specific - I eventually figured out a solution for IOS by following this post - http://pranavkhandelwal.com/blog/2015/11/1/detecting-user-inactivityidle-time-for-xamarinios.
For Android - I followed the advice on this forum https://forums.xamarin.com/discussion/149583/auto-logout-after-x-minutes-due-to-idle-inactivity-in-android. But I found that it wasn't handling when the device went to sleep. I've added in an override for the OnPause method and handling it in the OnResume method - my code below.
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
internal static MainActivity Instance { get; private set; }
public static long DISCONNECT_TIMEOUT = 60000; // 1 min = 1 * 60 * 1000 ms
public Handler disconnectHandler;
public DateTime pauseDate;
public bool deviceAsleep = false;
protected override void OnCreate(Bundle bundle)
{
switch (Preferences.Get("ORIENTATION", "Both").Trim().ToUpper())
{
case "PORTRAIT": RequestedOrientation = ScreenOrientation.Portrait; break;
case "LANDSCAPE": RequestedOrientation = ScreenOrientation.Landscape; break;
}
string timeo = Preferences.Get("TIMEOUT", "10");
int setTimeOut;
if (Int32.TryParse(timeo, out setTimeOut))
{
if (setTimeOut < 1)
{
setTimeOut = 10;
}
}
else
{
setTimeOut = 10;
}
DISCONNECT_TIMEOUT = setTimeOut * 60 * 1000; // 1 min = 1 * 60 * 1000 ms
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.Window.RequestFeature(WindowFeatures.ActionBar);
// Name of the MainActivity theme you had there before.
// Or you can use global::Android.Resource.Style.ThemeHoloLight
base.SetTheme(Resource.Style.MainTheme);
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.SetFlags("Visual_Experimental");
global::Xamarin.Forms.Forms.Init(this, bundle);
global::Xamarin.Forms.FormsMaterial.Init(this, bundle);
Rg.Plugins.Popup.Popup.Init(this, bundle);
disconnectHandler = new Handler(new MyHandlerICallback(this));
LoadApplication(new App());
Instance = this;
}
public class MyHandlerICallback : Java.Lang.Object, Handler.ICallback
{
private MainActivity mainActivity;
public MyHandlerICallback(MainActivity mainActivity)
{
this.mainActivity = mainActivity;
}
public bool HandleMessage(Message msg)
{
//ToDo
return true;
}
}
System.Action action = () =>
{
// Perform any required operation on disconnect
// Close the application
Android.OS.Process.KillProcess(Process.MyPid());
};
public void resetDisconnectTimer()
{
disconnectHandler.RemoveCallbacks(action);
disconnectHandler.PostDelayed(action, DISCONNECT_TIMEOUT);
}
public void stopDisconnectTimer()
{
disconnectHandler.RemoveCallbacks(action);
}
protected override void OnStop()
{
base.OnStop();
stopDisconnectTimer();
}
protected override void OnPause()
{
base.OnPause();
deviceAsleep = true;
// handle screen going to sleep
// record the date and time here
pauseDate = DateTime.Now;
stopDisconnectTimer(); // stop the timer while device is asleep
}
protected override void OnResume()
{
base.OnResume();
if (deviceAsleep)
{
// if device was asleep
// check the length it was asleep
deviceAsleep = false;
TimeSpan span = DateTime.Now.Subtract(pauseDate);
if ((span.Minutes * 60 * 1000) >= DISCONNECT_TIMEOUT)
{
// Close the application
Android.OS.Process.KillProcess(Process.MyPid());
}
}
resetDisconnectTimer();
}
public override void OnUserInteraction()
{
resetDisconnectTimer();
}
}
}
I'm just wondering could I have done this better or is there any other alternative. As I said, I've been working on this for a couple of days and even though there are lot of posts on user inactivity - none of the solutions I found handles both user inactivity and device sleep. I'm not an Android or IOS developer so a lot of the posts that I found were hard to follow.
I'm working my way though XAMARIN - by no means an expert so any advice is greatly appreciated.
Thanks