How to tell if application is open

3

I would like to know if you can check if the application is open, with a user using it or if it is in the background and it is using another application.

Depending on whether you have it open and user using it I will do a function, if it is not going to run another command.

    
asked by anonymous 16.03.2015 / 20:17

4 answers

1

Try using this code.

private boolean verifyApplicationRunning(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
        for (int i = 0; i < procInfos.size(); i++) {
            if (procInfos.get(i).processName.equals(NOME_DO_PACOTE)) {
                onDestroy();
                return true;
            }
        }
        return false;
    }
    
18.03.2015 / 20:24
1

Removed from this SOen response :

You can take advantage of the activities lifecycle and maintain a flag indicating whether there is a foreground activity. At the end of a transition from one activity to another this flag will have the value true ; if the activity is for background, it will have false value.

First, create a class responsible for managing the flag that indicates whether any application activity is in the foreground or not.

public class MyApplication extends Application {

  public static boolean isActivityVisible() {
    return activityVisible;
  }  

  public static void activityResumed() {
    activityVisible = true;
  }

  public static void activityPaused() {
    activityVisible = false;
  }

  private static boolean activityVisible;
}

Note: This class is not required to extend Application ; just use any class with methods globally accessible by the activities. If you choose to extend Application , be sure to declare the class in AndroidManifest.xml like this:

<application
    android:name="pacote.do.aplicativo.MyApplication"
    ...>

Second, add onPause() and onResume() methods to each of the project activities. You can choose to create a parent class that implements these callbacks and make the other activities extend this parent class.

@Override
protected void onResume() {
  super.onResume();
  MyApplication.activityResumed();
}

@Override
protected void onPause() {
  super.onPause();
  MyApplication.activityPaused();
}

Now, whenever you want to know if there is a foreground activity, just check by calling MyApplication.isActivityVisible() . The activities life cycle itself will keep this status updated.

For perfect operation I recommend doing this check always on the main thread. If you check within the onReceive() method of a broadcast receiver this is already guaranteed, now if you do this within a IntentService try to use a Handler .

Restrictions for this approach:

  • You need to include calls in the lifecycle ( onPause , onResume ) callbacks of all your application's activities, or in an activity-parent that is extended by the others. If you have activities extending ListActivity or MapActivity , you will have to add the methods to them individually.

  • For the check to be accurate you should call the check method from the main thread (if it is called on a secondary thread it may happen to catch a false value just in the middle of a transition between activities, when the flag transits briefly between the values true and false ).

19.03.2015 / 00:24
0

To implement what you want, we need to understand a bit about the life cycle of a Activity .

  • onCreate() is where you initialize your Activity . Here you call the setContentView() method with your .xml file and through findViewById() you reference your widgets.

  • onStart() is called when its Activity is visible to the user. This method can be called multiple times, including after onCreate() )

  • onResume() is called when when its Activity will initiate the interaction with the user.

  • onPause() , unlike onResume() is called when the system is about to start another Activity already created. Here is usually used to save / persist data, stop animations etc.

  • onStop() , unlike onStart() is called when its Activity is no longer visible to the user, since another Activity is being summarized and is covering yours.

Now, you simply understand and analyze what best fits your need to trigger your method.

References:

link link

    
17.03.2015 / 15:28
0

You can use a class to check if the app is in the background with ActivityManager.getRunningAppProcesses() .

class ChecarSegundoPlano extends AsyncTask<Context, Void, Boolean> {

  @Override
  protected Boolean doInBackground(Context... params) {
    final Context context = params[0].getApplicationContext();
    return isAppOnForeground(context);
  }

  private boolean isAppOnForeground(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null) {
      return false;
    }
    final String packageName = context.getPackageName();
    for (RunningAppProcessInfo appProcess : appProcesses) {
      if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
        return true;
      }
    }
    return false;
  }
}

// Execute com isto. Quando foregroud for true, sua aplicação estará em primeiro plano.
boolean foregroud = new ChecarSegundoPlano().execute(context).get();

Law more ...

    
18.03.2015 / 21:56