Running activities and services in the Android system

Source: Internet
Author: User

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.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.