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);
02
// get the info from the currently running task
03
List < ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(
1
);
04
05
Log.d(
"current task :"
,
"CURRENT Activity ::"
+ taskInfo.get(
0
).topActivity.getClass().getSimpleName());
06
07
ComponentName componentInfo = taskInfo.get(
0
).topActivity;
08
//if app is running
09
if
(componentInfo.getPackageName().equalsIgnoreCase(*Package Name*))
10
{
11
//do the implementation for if your app is running
12
}
Reference:
http://developer.android.com/reference/android/app/ActivityManager.html
01 | ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE); |
02 | // get the info from the currently running task |
03 | List < ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks( 1 ); |
04 |
05 | Log.d( "current task :" , "CURRENT Activity ::" + taskInfo.get( 0 ).topActivity.getClass().getSimpleName()); |
06 |
07 | ComponentName componentInfo = taskInfo.get( 0 ).topActivity; |
08 | //if app is running |
09 | if (componentInfo.getPackageName().equalsIgnoreCase(*Package Name*)) |
10 | { |
11 | //do the implementation for if your app is running |
12 | } |
0 comments:
Post a Comment