1

Is there a way to detect when an app is going into pause state or checking which app is currently displayed or running on the screen?

I know I can get a list of running apps but I want to know when an app(not my app) is going into pause state or which is app is displayed on the screen.

Regards

Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85
Naruto
  • 1,710
  • 7
  • 28
  • 39

1 Answers1

0

If u want to check continuously start background thread.

private Handler handler;
private Runnable _runnable;

private void startThread() {
    handler=new Handler();
    _runnable = new Runnable() {
        public void run() {
            checkRunningApplications();
            handler.postDelayed(_runnable, 500);
        }
    };
    _runnable.run();
}

private void checkRunningApplications() {
    ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    List<RunningTaskInfo> runningTasks = activityManager.getRunningTasks(1);
    RunningTaskInfo runningTaskInfo = runningTasks.get(0);
    ComponentName topActivity = runningTaskInfo.topActivity;
    System.out.println(topActivity.getPackageName());
}

Mention below permission in Manifest file:

<uses-permission android:name="android.permission.GET_TASKS" />
jagmohan
  • 2,052
  • 2
  • 26
  • 41
user543
  • 3,623
  • 2
  • 16
  • 14