开发者

How to find top most Activity for a Task?

开发者 https://www.devze.com 2023-04-04 16:09 出处:网络
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 currentl

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.

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 >= 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);
  }
};

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?

Thanks.


Make all your Activities subclasses of a common superclass with common onPause and onResume methods that maintain a “TopmostActivity” variable, perhaps as a static field of the superclass.

0

精彩评论

暂无评论...
验证码 换一张
取 消