androidactivity-manageractivity-monitor

Detect if another app is running


Is it possible on Android to detect if another app is running? I need to trigger some action in my app when a Map application (like Waze or Google Maps) is running.


Solution

  • You can use ActivityManager class:

    final   ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    final List<RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
    
        for (int i = 0; i < recentTasks.size(); i++) 
        {
            Log.d("Executed app", "Application executed : " +recentTasks.get(i).baseActivity.toShortString()+ "\t\t ID: "+recentTasks.get(i).id+"");         
        }
    

    Please be aware that this capability was deprecated in API level-21.

    An alternative could be getRunningAppProcesses() method of ActivityManager.