String myPacket = activity.getPackageName();
String packageName = myPacket ;// "com.example.testant";
ActivityManager activityManager = (ActivityManager) activity
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
if (tasksInfo.size() > 0) {
String topPacketName = tasksInfo.get(0).topActivity
.getPackageName();
Log.d("istop", "topPacketName==" + topPacketName);
// 应用程序位于堆栈的顶层
if (packageName.equals(topPacketName)) {
return true;
}
}
return false;
}
添加权限 <uses-permission android:name="android.permission.GET_TASKS" />
public static boolean isApplicationShowing(String packageName,
Context context) {
boolean result = false;
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = am.getRunningAppProcesses();
if (appProcesses != null) {
for (RunningAppProcessInfo runningAppProcessInfo : appProcesses) {
if (runningAppProcessInfo.processName.equals(packageName)) {
int status = runningAppProcessInfo.importance;
if (status == RunningAppProcessInfo.IMPORTANCE_VISIBLE
|| status == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
result = true;
break;
}
}
}
}
return result;
}