【问题标题】:Get current visible activity to user获取用户当前可见的活动
【发布时间】:2013-09-06 21:32:51
【问题描述】:

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

activityManager = (ActivityManager) FWCommon.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
runningTaskInfoList = activityManager.getRunningTasks(1);
componentName = runningTaskInfoList.get(0).topActivity;

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

以下是问题

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

谢谢 达尔文

【问题讨论】:

  • 对不起,我放了一个自定义类代码,考虑下面的代码来获取前台活动。 activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); runningTaskInfoList = activityManager.getRunningTasks(1); componentName = runningTaskInfoList.get(0).topActivity;

标签: android


【解决方案1】:

尝试使用 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
    }
}

点击herehere了解更多信息。

【讨论】:

  • 有什么方法可以检查它是否在主屏幕上?
猜你喜欢
  • 1970-01-01
  • 2014-10-30
  • 1970-01-01
  • 2014-12-22
  • 1970-01-01
  • 1970-01-01
  • 2013-03-14
  • 2017-09-02
  • 1970-01-01
相关资源
最近更新 更多