判断程序在前台还是退到后台的方法
2014-12-04 17:20
141 查看
方法一:
public static boolean isOnBackground(Context context)
{
String packageName = context.getPackageName();
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
if (tasksInfo.size() > 0)
{
// 应用程序位于堆栈的顶层
if (packageName.equals(tasksInfo.get(0).topActivity.getPackageName()))
{
return false;
}
}
return true;
}
方法二:
public static boolean isAppOnForeground(Context context)
{
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
String packageName = context.getPackageName();
// Returns a list of application processes that are running on the device
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null)
return false;
for (RunningAppProcessInfo appProcess : appProcesses)
{
// The name of the process that this object is associated with.
if (appProcess.processName.equals(packageName)
&& appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
{
return true;
}
}
return false;
}
public static boolean isOnBackground(Context context)
{
String packageName = context.getPackageName();
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
if (tasksInfo.size() > 0)
{
// 应用程序位于堆栈的顶层
if (packageName.equals(tasksInfo.get(0).topActivity.getPackageName()))
{
return false;
}
}
return true;
}
方法二:
public static boolean isAppOnForeground(Context context)
{
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
String packageName = context.getPackageName();
// Returns a list of application processes that are running on the device
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null)
return false;
for (RunningAppProcessInfo appProcess : appProcesses)
{
// The name of the process that this object is associated with.
if (appProcess.processName.equals(packageName)
&& appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
{
return true;
}
}
return false;
}
相关文章推荐
- 判断程序是前台运行还是后台运行的方法?
- 6种方法来判断App处于前台还是后台
- Android判断当前应用程序处于前台还是后台的两种方法
- (4.2.16.2)Android判断当前应用程序处于前台还是后台的两种方法
- Android如何判断程序是在前台还是在后台运行
- Android 判断程序在前台运行还是后台运行
- Android判断程序是前台还是后台
- Android判断当前应用程序处于前台还是后台的两种方法
- 判断程序在后台运行还是前台
- Android 判断程序在前台还是后台
- Android判断当前应用程序处于前台还是后台的两种方法
- iOS判断程序在前台还是后台
- Android判断当前应用程序处于前台还是后台的两种方法
- Android判断当前应用程序处于前台还是后台的两种方法
- android判断当前应用程序处于前台还是后台的两种方法
- Android判断当前应用程序处于前台还是后台的两种方法
- Android判断当前应用程序处于前台还是后台的两种方法
- Android判断当前应用程序处于前台 4000 还是后台的实现方法
- Android之判断程序在前台运行还是后台运行
- iOS判断程序在前台还是后台