1. 程式人生 > >ActivityManager獲得正在執行的activity與service

ActivityManager獲得正在執行的activity與service

            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());
                }