问题
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:
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
.
来源:https://stackoverflow.com/questions/28652076/detect-if-another-app-is-running