-3

I want to create a handler or what ever to check if the app is still running, something like

while(appIsRunning)
 {

 //do something

 }
do somthimg
Amalo
  • 772
  • 3
  • 13
  • 32
  • Check this Link it will solve your problem http://stackoverflow.com/questions/4212992/how-can-i-check-if-an-app-running-in-android – Ashish Shukla Sep 15 '15 at 12:36
  • possible duplicate of [Checking if an Android application is running in the background](http://stackoverflow.com/questions/3667022/checking-if-an-android-application-is-running-in-the-background) – Abhinav Singh Maurya Sep 15 '15 at 12:36
  • Is this one app checking another running? – weston Sep 15 '15 at 12:55
  • nop i need to check if my current app is still running or opened – Amalo Sep 15 '15 at 12:55
  • @Amalo you should be more specific of what you want to achieve, what is the problem, where you have to check that your app is "running" (you can edit your post to add informations) If some code is running it's because your app is running, I don't know what you mean by "how to check if my app is still runing" – Hacketo Sep 15 '15 at 13:07
  • okay i will explain more – Amalo Sep 15 '15 at 14:05

2 Answers2

1

Use "ActivityLifecycleCallbacks" and use it this way:

    public class MyApplication extends Application {

     @Override
     public void onCreate (){
     super.onCreate();
     registerActivityLifecycleCallbacks(new MyActivityLifecycleCallbacks());
     }

        @Override
        public void onTerminate (){
         super.onTerminate();
        }

        @Override
        public void onConfigurationChanged (Configuration newConfig){
         super.onConfigurationChanged(newConfig);
        }

        private static final class MyActivityLifecycleCallbacks implements ActivityLifecycleCallbacks {

      public void onActivityCreated(Activity activity, Bundle bundle) {
       Log.e("","onActivityCreated:" + activity.getLocalClassName());
      }

      public void onActivityDestroyed(Activity activity) {
       Log.e("","onActivityDestroyed:" + activity.getLocalClassName());
      }

      public void onActivityPaused(Activity activity) {
       Log.e("","onActivityPaused:" + activity.getLocalClassName());
      }

      public void onActivityResumed(Activity activity) {
       Log.e("","onActivityResumed:" + activity.getLocalClassName());
      }

      public void onActivitySaveInstanceState(Activity activity,
        Bundle outState) {
       Log.e("","onActivitySaveInstanceState:" + activity.getLocalClassName());
      }

      public void onActivityStarted(Activity activity) {
       Log.e("","onActivityStarted:" + activity.getLocalClassName());
      }

      public void onActivityStopped(Activity activity) {
       Log.e("","onActivityStopped:" + activity.getLocalClassName());
      }
        }
    }

This way you can check each state and do anything...

Mariano Zorrilla
  • 7,165
  • 2
  • 34
  • 52
-1

Try This

public static boolean isAppRunning(Context context) 
    {
        ActivityManager activityManager = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> appProcesses = activityManager
            .getRunningAppProcesses();
        for (RunningAppProcessInfo appProcess : appProcesses) 
         {
            if (appProcess.processName.equals(context.getPackageName())) 
            {
                if (appProcess.importance != RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE) 
                {
                    return true;
                } 
            }
        }
        return false;
    }
Kiran Choudhary
  • 1,125
  • 7
  • 15
  • how this is related to check if an app is running or not ? – Hacketo Sep 15 '15 at 12:38
  • 1
    @Hacketo can you help me instead of voting down answers ?:D – Amalo Sep 15 '15 at 12:55
  • @KiranChoudhary the original answer was not related to the issue, and the edit is a copy/paste of this [answer](http://stackoverflow.com/questions/3667022/checking-if-an-android-application-is-running-in-the-background#answer-29158852) without any explanation – Hacketo Sep 15 '15 at 13:04