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");
            }
        }
}

1 comment:

  1. Thanks for helping me to understand Way to check whether an application is running in Android . As a beginner in Android programming your post help me a lot. Android Training in chennai | Android Training institute in chennai

    ReplyDelete