Skip to content

Instantly share code, notes, and snippets.

@prashantvc
Created January 18, 2013 15:35
Show Gist options
  • Save prashantvc/4565376 to your computer and use it in GitHub Desktop.
Save prashantvc/4565376 to your computer and use it in GitHub Desktop.
Periodically checks if app is idle for particular interval. (MonoTouch)
public class ActiveWindows:UIWindow
{
NSTimer idleTimter;
double idleTimeInterval;
public MyWindows (RectangleF frame):base(frame)
{
idleTimeInterval = 10; //Change the idle time
idleTimter =
NSTimer.CreateScheduledTimer (idleTimeInterval, WindowIdleNotification);
}
public override void SendEvent (UIEvent evt)
{
base.SendEvent (evt);
var allTouches = evt.AllTouches;
if (allTouches.Count > 0) {
var touch = allTouches.AnyObject as UITouch;
var phase = touch.Phase;
if (phase == UITouchPhase.Began || phase == UITouchPhase.Ended) {
if (!idleTimter.IsValid) {
WindowActiveNotification ();
} else {
idleTimter.Invalidate ();
}
if (idleTimeInterval != 0) {
idleTimter =
NSTimer.CreateScheduledTimer (idleTimeInterval, WindowIdleNotification);
}
}
}
}
void WindowActiveNotification ()
{
Console.WriteLine ("Active");
}
void WindowIdleNotification ()
{
Console.WriteLine ("Idle");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment