如何获取移动前台中显示的应用程序?

时间:2012-12-17 07:43:43

标签: android service

我想知道服务中当前手机界面上显示的活动。

android中有活动堆栈。它是否在系统中有一个全局活动堆栈并提供前台显示的活动信息?

1 个答案:

答案 0 :(得分:2)

Context context = null; // FIXME: init with your context.
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
        for (int i = 0; i < procInfos.size(); i++) {
            if (procInfos.get(i).importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                //the current running process
                Log.d("FOREGROUND_Application", procInfos.get(i).processName + "is running on the FOREGROUND");
            }
        }