ActivityManager获得正在运行的activity与service

时间:2022-12-16 05:00:42
            ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE) ;  

//获得系统运行的进程
List<ActivityManager.RunningAppProcessInfo> appList1 = mActivityManager
.getRunningAppProcesses();
for (RunningAppProcessInfo running : appList1) {
System.out.println(running.processName);
}
System.out.println("================");

//获得当前正在运行的service
List<ActivityManager.RunningServiceInfo> appList2 = mActivityManager
.getRunningServices(100);
for (ActivityManager.RunningServiceInfo running : appList2) {
System.out.println(running.service.getClassName());
}

System.out.println("================");

//获得当前正在运行的activity
List<ActivityManager.RunningTaskInfo> appList3 = mActivityManager
.getRunningTasks(1000);
for (ActivityManager.RunningTaskInfo running : appList3) {
System.out.println(running.baseActivity.getClassName());
}
System.out.println("================");

//获得最近运行的应用
List<ActivityManager.RecentTaskInfo> appList4 = mActivityManager
.getRecentTasks(100, 1);
for (ActivityManager.RecentTaskInfo running : appList4) {
System.out.println(running.origActivity.getClassName());
}