«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

올해는 머신러닝이다.

[펌]Android: Check application is running 본문

Android/Tip&Tech

[펌]Android: Check application is running

행복한 수지아빠 2011. 9. 21. 19:27
출처 : http://kpbird.blogspot.com/2011/08/android-check-application-is-running.html

Android: Check application is running ?

| Wednesday, August 17, 2011
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