How to detect if your android app is in the foreground (API 21)
问题 when receiving a GCM message, the behaviour of my app depends on if it is in the foreground or not. Prior to API 21 I used the following: Boolean onForeground = this.getPackageName().equalsIgnoreCase( ((ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE)) .getRunningTasks(1).get(0).topActivity.getPackageName() ); Now getRunningTasks() is deprecated and behaving differently on API 21, what would be the best way on API 21 to detect if my app is in the foreground? Google recommends