---
title: "android 如何判断程序是否在前台运行"
author: "crazier9527"
date: "2012-04-06T00:04:28Z"
tags: []
categories: ["移动开发"]
slug: "android"
draft: false
lastmod: "2014-04-22T16:06:41Z"
id: 1476170
url: "https://crazier9527.iteye.com/blog/1476170"
---

01. private boolean isTopActivity(){
02. List&lt;RunningTaskInfo&gt;  tasksInfo = activityManager.getRunningTasks(1);
03. if(tasksInfo.size() &gt; 0){
04. //应用程序位于堆栈的顶层
05. if(packageName.equals(tasksInfo.get(0).topActivity.getPackageName())){
06. return true;
07. }
08. }
09. return false;
10. }

感谢：[http://www.mangocd.com/](http://www.mangocd.com)