Way to check whether an application is running in Android

Today I came across one interesting thing in Android and I would like to share with you all. In this post I am going to explain the power of the class RunningAppProcessInfo. Through this class we can check whether the application is running, if it is running what is the importance of it, like whether it is in foreground, background, visible etc.

The code snippet is,

ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningProcInfo = activityManager .getRunningAppProcesses();
for(int i = 0; i < runningProcInfo.size(); i++){
        if(runningProcInfo.get(i).processName.equals("com.android.XXXXX")) {
            Log.i(TAG, "XXXX is running");
        }
}


To check the omportance of the process, that is whether it is foreground or in background etc, we can check through the field lru.

for(int i = 0; i < runningProcInfo.size(); i++){
        if(runningProcInfo.get(i).processName.equals("com.android.XXXXX")) {
            Log.i(TAG, "XXXX is running");
            if (runningProcInfo.get(i).lru==RunningAppProcessInfo.IMPORTANCE_FOREGROUND){
                    Log.i(TAG, "XXXX is in foreground");
            }
        }
}

Comments

Popular posts from this blog

AlarmManager and Notification in Android

Swipe Action and ViewFlipper in Android

Open Gallery and get the selected image in appplication