Do you really want to know that the system is running the activity in the background in the current status? Here, let's implement it together.
/** <Br/> * obtain information about running tasks in the system. (The task is a collection of multiple activities) <br/> */<br/> Public void run () {<br/> string info = ""; <br/> // obtain the maximum number of running activities (preventing too many activities and the system cannot afford them) and <br/> int maxnum = 40; <br/> // The activitymanager function provides interfaces for all running activity interactions in the system. The main interfaces are centered around the running process information and task information, service Information <br/> activitymanager = (activitymanager) This. getsystemservice (activity_service); <br/>/** <br/> * When obtaining a running task, pay attention to it. <br/> * Your task or activity may not end, but when you use it later, it is very likely that it has been killed. <Br/> * The meaning is very simple. The running task that the system returns to you is transient. It only indicates the status in the system when you call this method, <br/> * The system is not responsible for the change. <br/> */<br/> List <activitymanager. runningtaskinfo> runningtasks = activitymanager. getrunningtasks (maxnum); <br/> for (activitymanager. runningtaskinfo taskinfo: runningtasks) {<br/> info + = "Start of a task:/N "; <br/> info + = "Number of running activities in the current task:" + taskinfo. numrunning; <br/> info + = "number of activities in the current task:" + taskinfo. numactivities; <br/> info + = "Name of the activity that starts the current task:" + taskinfo. baseactivity. getclassname (); </P> <p>}
Note: The permission for Android. Permission. get_tasks is required.
In the above code, we can only get the running task. How can we get the service information that is running in the background?
See the Code:
/*** <Br/> * obtain the service running in the background <br/> */<br/> private void run2 () {<br/> string info = ""; <br/> int maxnum = 30; <br/> activitymanager = (activitymanager) getsystemservice (activity_service ); <br/> // note that the methods obtained and return value types have changed. <br/> List <activitymanager. runningserviceinfo> Serivces = activitymanager. getrunningservices (maxnum); <br/> for (runningserviceinfo service: Serivces) {<br/> info = "Start of a service:/N "; <br/> info + = "Start Time of the current service:" + service. activesince; <br/> info + = "number of clients linked to the current service:" + service. clientcount; <br/>}</P> <p>}
Bytes ---------------------------------------------------------------------------------------
Next, we will show the information we can obtain for the runningtaskinfo and runningserviceinfo classes.