How to find top most Activity for a Task?
<p>I have an app where I'm running a timer event in the background checking for inactivity. When a certain amount of time has passed, I want to call the finish() method for whatever activity is currently top most for my app (task). Note, that my app can be in the background so I can't just use the top most active activity on the device. My problem, is finding the top most activity for my task, and then calling the finish() method for that activity. Below is a snippet of my Runnable timer that I'm using. The "activity" variable in my snippet is the thing that I can't seem to find.</p>

<pre><code>mUpdateTimeTask = new Runnable() 
{
  public void run() 
  {
     // - for every 10 seconds, this runnable task will check the inactivity time and will exit this app
     //   if the time allocated is exceeded

     long longNowTime = System.currentTimeMillis()/1000;

     if ( longNowTime-globalVariables.longLastActivityTime &gt;= globalVariables.intLockoutDuration )
     {                 
         // - first, we'll stop the lockout timer from running

         mHandler.removeCallbacks(mUpdateTimeTask);

         // - call the finish() method to close the top most activity for this task (app)

         activity.finish();
     }

     mHandler.postDelayed(this,mIntInterval);
  }
};
</code></pre>

<p>Can anyone offer some suggestions to how I can get the Activity object for my top most activity and call the finish() method to close it?</p>

<p>Thanks.</p>
