Detect if another app is running

2019-04-14 20:08发布

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.

1条回答
Bombasti
2楼-- · 2019-04-14 21:06

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.

查看更多
登录 后发表回答