As far as I know, there is no direct API to detect whether HOME is in foreground or background.
But the following snippet will tell you whether the user is currently at HOME or not.
if (mActivityManager.getRunningTasks().get(0).topActivity.getPackageName().equals("com.android.launcher") {
//home
}
DISCLAIMER: The above code will work only for the default launcher (com.android.launcher)
. So, if the user is using some third-party launcher, you can't detect that if you don't know the package name of that launcher.