How to check android application in background or not?

Check if application in background or not snippet

public static boolean isApplicationInBackground(Context context) {
        ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
        List taskList = am.getRunningTasks(1);
        if (taskList != null && !taskList.isEmpty()) {
            ComponentName topActivity = taskList.get(0).topActivity;
            if (topActivity != null && !topActivity.getPackageName().equals(context.getPackageName())) {
                return true;
            }
        }
        return false;
} 

whether the application is in the background we need to define permission android.permission.GET_TASKS in Manifest.xml


This function will return true if the application in the background otherwise it will return false.

Comments

Popular posts from this blog

Android studio short cuts lists

Android status bar and navigation bar height