您的位置:首页 > 移动开发

android 判断app是否处于前台

2016-01-28 16:23 225 查看
逻辑是获取系统的运行栈,处于栈顶的就是处于前台的app,然后对比包名,不同则表示app处于后台,反之,则表示app处于前台.

/**
* app是否在后台
*
* @param context
* @return true 是 false 不是
*/
public static boolean isBackground(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
StaticMethod.debugEMSG(topActivity.getPackageName() + " : " + context.getPackageName());
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: