获取当前可见的活动给用户

Dal*_*ngh 4 android

我想让前台活动运行.我可以通过使用以下代码使用活动管理器来获取此信息.

activityManager = (ActivityManager) FWCommon.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
runningTaskInfoList = activityManager.getRunningTasks(1);
componentName = runningTaskInfoList.get(0).topActivity;
Run Code Online (Sandbox Code Playgroud)

假设我有两个活动A和B,我从活动A和B的onResume()调用上面的代码.

以下是问题所在

  1. 当活动A在代码上面开始时,我给topActivity = A.
  2. 然后我从活动A移动到B,上面的代码给我topActivity = B.
  3. 然后我按Back返回从活动B回到A和上面的代码再次给我topActivity = B而不是A.

Thx Dalvin

A. *_*iri 9

尝试使用getRunningAppProcesses()来获取RunningAppProcessInfo的列表.然后通过执行以下操作遍历每个RunningAppProcessInfo并检查它是否在前台:

List<ActivityManager.RunningAppProcessInfo> processes = manager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo process : processes)
{
    // Take a look at the IMPORTANCE_VISIBLE property as well in the link provided at the bottom
    if (process.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
    {
        // Put code here
    }
}
Run Code Online (Sandbox Code Playgroud)

点击此处此处获取更多信息.