Android has different application lifecycle, Many times user press Home hardware button and application goes in background, Application is still running but it's in background, Now We want to identify that application is running in background or not. It require when we don't want to create new instance of Activity. Android SDK provide class named ActivityManager. Using ActivityManager we can check application is running in background.
Here is the simple code for the same. you just need to change package name as per your requirement.
01 |
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE); |
03 |
List < ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks( 1 ); |
05 |
Log.d( "current task :" , "CURRENT Activity ::" + taskInfo.get( 0 ).topActivity.getClass().getSimpleName()); |
07 |
ComponentName componentInfo = taskInfo.get( 0 ).topActivity; |
09 |
if (componentInfo.getPackageName().equalsIgnoreCase(*Package Name*)) |
Reference:
http://developer.android.com/reference/android/app/ActivityManager.html