调用此方法:
//当前应用是否处于前台
private boolean isForeground(Context context) {
if (context !=null) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
String currentPackageName = cn.getPackageName();
if (!TextUtils.isEmpty(currentPackageName) && currentPackageName.equals(context.getPackageName())) {
return true;
}
return false;
}
return false;
}
网友评论