码迷,mamicode.com
首页 > 其他好文 > 详细

判断当前应用是否在后台运行

时间:2015-05-04 15:39:31      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:android开发   工具类   

/**判断程序是否在后台运行*/

public static boolean isRunBackground(Context context) {

ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.processName.equals(context.getPackageName())) {
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
//表明程序在后台运行
return true;
}
else {
return false;
}
}
}
return false;

}

/**判断程序是否在前台运行(当前运行的程序)*/

public boolean isRunForeground() {
ActivityManager activityManager = (ActivityManager)getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
String packageName = getApplicationContext().getPackageName();
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null) 

                   return false;

for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.processName.equals(packageName) && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { 

                            return true;//程序运行在前台

        }
}
return false;
}

判断当前应用是否在后台运行

标签:android开发   工具类   

原文地址:http://blog.csdn.net/true100/article/details/45479827

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!