Skip to content

Instantly share code, notes, and snippets.

@harshvishu
Last active August 29, 2015 14:27
Show Gist options
  • Save harshvishu/7f8c36c085ab515e4fa5 to your computer and use it in GitHub Desktop.
Save harshvishu/7f8c36c085ab515e4fa5 to your computer and use it in GitHub Desktop.
The method 'isAppForeground()' of this class returns true if application is currently visible otherwise false
package com.package.application.*;//your own package
import android.app.ActivityManager;
import android.content.Context;
import java.util.List;
/**
* Created by harsh on 13/8/2015.
* <p/>
* Class to check whether the application is in foreground or not
*/
public class GetApplicationStatus {
Context context;
public GetApplicationStatus(Context context) {
this.context = context;
}
public boolean isAppForeground() {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null) {
return false;
}
final String packageName = context.getPackageName();
ActivityManager.RunningAppProcessInfo appProcess = appProcesses.get(0);//generally it will not (never) return null
return appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment