Extending Activity to implement an Idle timer
<p>I've developed some code that implements a timer - it ticks every second and after x seconds of not touching the display the "idle" activity is launched.
However, this needs to be implemented for about 8 activities... I could copy and paste the relevant code to each activity but that's not an elegant solution.</p>

<p>So, I started to create my custom activity "TimedActivity" to extend Activity. The other activities that I'll need to implement can then implement "TimedActivity".</p>

<p>However, how can my TimedActivity detect onTouch events ?</p>

<p>this is what I have so far:</p>

<pre><code>public class TimedActivity extends Activity implements OnTouchListener
{
    private LinearLayout mLinearLayoutMain;//intended to be the root node in the XML
    private Timer mTimerSeconds;
    private int mIntIdleSeconds;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //NO setContentView() 

        mIntIdleSeconds=0;

        mTimerSeconds = new Timer();
        mTimerSeconds.schedule(new TimerTask()
        {
            @Override
            public void run()
            {
                timerSecondsCounter();
            }
        }, 0, 1000);

        //the whole screen should become sensitive to touch
        //HOWEVER, there's no View established
        mLinearLayoutMain = (LinearLayout) findViewById(R.id.layout_main);
        //mLinearLayoutMain remains null, so next line excepts
        mLinearLayoutMain.setOnTouchListener(this);
    }


    @Override
    protected void onDestroy()
    {
        if (mTimerSeconds != null)
        {
            mTimerSeconds.cancel();
        }
        super.onDestroy();
    }


    public boolean onTouch(View v, MotionEvent event)
    {
        mIntIdleSeconds=0;
        return false;//do not consume
    }

    private void timerSecondsCounter()
    {
        mIntIdleSeconds++;

        if (mIntIdleSeconds == Constants.MAX_IDLE_TIME_SECONDS)
        {
            final Intent intent = new Intent(this, what.ever.com.Idle.class);
            startActivity(intent);
        }
    }
}
</code></pre>

<p>the subsequent activity to use this will then use:</p>

<pre><code>public class ActivitySelect extends TimedActivity
{
}
</code></pre>

<p>and the xml for the view would be:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/layout_main"&gt;

&lt;!-- MORE STUFF ---&gt;

&lt;/LinearLayout&gt;
</code></pre>

<p>I feel that I am close, but what do I need to do to get this working ?</p>

<p><strong>EDIT:
This is what I ended up implementing...</strong></p>

<pre><code>public class TimedActivity extends Activity
{
    //member variables
    private Timer mTimerSeconds;
    private int mIntIdleSeconds;
    private boolean mBoolInitialized=false;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onDestroy()
    {
        if (mTimerSeconds != null)
        {
            mTimerSeconds.cancel();
        }
        super.onDestroy();
    }

    public void onTouch()
    {
        mIntIdleSeconds=0;
    }

    /** start the idle timer */
    public void startIdleTimer()
    {
        if (mBoolInitialized == false)
        {
            mBoolInitialized = true;

            //initialize idle counter
            mIntIdleSeconds=0;

            //create timer to tick every second
            mTimerSeconds = new Timer();
            mTimerSeconds.schedule(new TimerTask()
            {
                @Override
                public void run()
                {
                    timerSecondsCounter();
                }
            }, 0, 1000);
        }
    }

    /** called every second to count idle time and to update clock on Welcome screen */
    private void timerSecondsCounter()
    {

        mIntIdleSeconds++;

        if (mIntIdleSeconds == Constants.MAX_IDLE_TIME_SECONDS)
        {
            //idle time long enough to launch standby activity
            final Intent intent = new Intent(this, what.ever.com.Idle.class);
            startActivity(intent);
        }

    }

}
</code></pre>

<p>To use this ActivityTimed, within your launched Activity do:</p>

<pre><code>public class ActivitySelect extends TimedActivity implements OnTouchListener
{
    //UI references
    private LinearLayout mLinearLayout;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);//triggers TimedActivity::onCreate()
        setContentView(R.layout.select);

        //get references to all of your widgets
        mLinearLayout = (LinearLayout) findViewById(R.id.select_linearlayout_main);

        //set your widgets as touchable
        mLinearLayout.setOnTouchListener(this);

        //start the idle timer
        super.startIdleTimer();
    }

    @Override
    protected void onDestroy()
    {
        //this is very important here ;-)
        super.onDestroy();
    }

    public boolean onTouch(View v, MotionEvent event)
    {
        final int actionPerformed = event.getAction();

        //reset idle timer
        // put this here so that the touching of empty space is captured too
        //  it seems that LinearLayout doesn't trigger a MotionEvent.ACTION_UP or MotionEvent.ACTION_MOVE
        if (actionPerformed == MotionEvent.ACTION_DOWN)
        {
            super.onTouch();
        }

        return false;//do not consume event!
    }
}
</code></pre>
