Monodroid Study Notes (13th) -- homemade task manager to get running programs and services

Source: Internet
Author: User

The Android operating system does not provide task management programs. It does not know which programs are running in the background, but provides a list of running services. This time, we will use monodroid to get the programs and services running on the mobile phone, and use listview to list them. Click the listview item to close the program or service.

I did not implement real-time retrieval here, but only obtained it by clicking two buttons. The layout file is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <button Android: Id = "@ + ID/btngettask" <br/> Android: TEXT = "running program" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content"/> <br/> <button Android: id = "@ + ID/btngetservice" <br/> Android: text = "Running Service" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_torightof = "@ ID/btngettask"/> <br/> <listview Android: id = "@ + ID/lvtasks" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: layout_below = "@ ID/btngettask"> </listview> <br/> </relativelayout> 

To obtain a running program, use the getrunningtasks method of the activitymanager class. Its Parameters specify the number of tasks to be retrieved. Due to limited resources, we have set up to 30 tasks to be retrieved:

Public class activity1: activity <br/>{< br/> List <string> List = new list <string> (); <br/> ilist <activitymanager. runningtaskinfo> listrunningtasks; <br/> activitymanager am; <br/> protected override void oncreate (bundle) <br/>{< br/> base. oncreate (bundle); <br/> setcontentview (resource. layout. main); <br/> button btngettask = findviewbyid <button> (resource. id. btngettask); <br/> button btng Etservice = findviewbyid <button> (resource. id. btngetservice); <br/> listview lvtasks = findviewbyid <listview> (resource. id. lvtasks); <br/> AM = This. getsystemservice (activityservice) as activitymanager; <br/> btngettask. click + = (sender, e) => <br/>{< br/> contentshown = 1; <br/> try <br/>{< br/> list. clear (); <br/> lvtasks. adapter = NULL; <br/> listrunningtasks = aM. getrunningtasks (30); <br/> int Index = 1; <br/> foreach (activitymanager. runningtaskinfo task in listrunningtasks) <br/>{< br/> list. add (string. format ("{0 }:{ 1} (ID = {2})", index ++, task. baseactivity. classname, task. ID); <br/>}< br/> arrayadapter <string> adapter = new arrayadapter <string> (this, android. resource. layout. simplelistitem1, list); <br/> If (adapter. count = 0) <br/>{< br/> toast. maketext (this, "No program is running currently", toastle Ngth. long ). show (); <br/>}< br/> else <br/> {<br/> lvtasks. adapter = adapter; <br/>}< br/> catch (securityexception) <br/>{< br/> toast. maketext (this, "You are not authorized to run the program", toastlength. long ). show (); <br/>}< br/> catch (system. exception ex) <br/>{< br/> MessageBox. showerrormessage (this, ex); <br/>}< br/>}; <br/> lvtasks. itemclick + = (sender, e) =>< br/>{< br/> MessageBox. confirm (this, "prompt", "Are you sure you want to close this program? ", (S2, E2) => <br/>{< br/> AM. restartpackage (listrunningtasks [E. position]. baseactivity. packagename); <br/> btngettask. optional mclick (); <br/>}, (S3, E3) =>{}); <br/> }; <br/>}< br/> 

Note that to obtain the task, you must go to androidmanifest. to add the get_tasks permission to XML and close the program, we use the restartpackage method of the activitymanager class to close the program according to the package name. Therefore, we need to add the restart_packages permission:

<Uses-Permission Android: Name = "android. permission. get_tasks "/> <br/> <uses-Permission Android: Name =" android. permission. restart_packages "/> <br/> 

:

 

The following describes how to obtain the running service results. Obtaining services is similar to obtaining programs. You only need to change getrunningtasks to getrunningservices. Similarly, we still need to retrieve 30 items. Because it is displayed in the same listview, When you click a list item, you need to distinguish whether the clicked item is a program or a service. Therefore, we add a field contentshown to distinguish it. When contentshown = 1, it indicates the program, contentshown = 2 indicates the service. The complete code is as follows:

Public class activity1: activity <br/>{< br/> List <string> List = new list <string> (); <br/> ilist <activitymanager. runningtaskinfo> listrunningtasks; <br/> ilist <activitymanager. runningserviceinfo> listrunningservices; <br/> activitymanager am; <br/> int contentshown = 0; <br/> protected override void oncreate (bundle) <br/>{< br/> base. oncreate (bundle); <br/> setcontentview (resource. layout. Main); <br/> button btngettask = findviewbyid <button> (resource. id. btngettask); <br/> button btngetservice = findviewbyid <button> (resource. id. btngetservice); <br/> listview lvtasks = findviewbyid <listview> (resource. id. lvtasks); <br/> AM = This. getsystemservice (activityservice) as activitymanager; <br/> btngettask. click + = (sender, e) => <br/>{< br/> contentshown = 1; <br/> try <br/>{< br/> list. Clear (); <br/> lvtasks. adapter = NULL; <br/> listrunningtasks = aM. getrunningtasks (30); <br/> int Index = 1; <br/> foreach (activitymanager. runningtaskinfo task in listrunningtasks) <br/>{< br/> list. add (string. format ("{0 }:{ 1} (ID = {2})", index ++, task. baseactivity. classname, task. ID); <br/>}< br/> arrayadapter <string> adapter = new arrayadapter <string> (this, android. resource. layout. simplelistite M1, list); <br/> If (adapter. count = 0) <br/>{< br/> toast. maketext (this, "No program is running currently", toastlength. long ). show (); <br/>}< br/> else <br/> {<br/> lvtasks. adapter = adapter; <br/>}< br/> catch (securityexception) <br/>{< br/> toast. maketext (this, "You are not authorized to run the program", toastlength. long ). show (); <br/>}< br/> catch (system. exception ex) <br/>{< br/> MessageBox. showerrormessage (this, ex); <br/>} <Br/>}; <br/> btngetservice. click + = (sender, e) => <br/>{< br/> contentshown = 2; <br/> try <br/>{< br/> lvtasks. adapter = NULL; <br/> listrunningservices = aM. getrunningservices (30); <br/> int Index = 1; <br/> list. clear (); <br/> foreach (activitymanager. runningserviceinfo service in listrunningservices) <br/>{< br/> list. add (string. format ("{0 }:{ 1} (ID = {2})", index ++, service. process, Serv Ice. PID); <br/>}< br/> arrayadapter <string> adapter = new arrayadapter <string> (this, android. resource. layout. simplelistitem1, list); <br/> If (adapter. count = 0) <br/>{< br/> toast. maketext (this, "No service is running currently", toastlength. long ). show (); <br/>}< br/> else <br/> {<br/> lvtasks. adapter = adapter; <br/>}< br/> catch (securityexception) <br/>{< br/> toast. maketext (this, "You are not authorized to run the service", Astlength. long ). show (); <br/>}< br/> catch (system. exception ex) <br/>{< br/> MessageBox. showerrormessage (this, ex); <br/>}< br/>}; <br/> lvtasks. itemclick + = (sender, e) => <br/>{< br/> If (contentshown = 1) <br/>{< br/> MessageBox. confirm (this, "prompt", "Are you sure you want to close this program? ", (S2, E2) => <br/>{< br/> AM. restartpackage (listrunningtasks [E. position]. baseactivity. packagename); <br/> btngettask. extends mclick (); <br/>}, (S3, E3) =>{}); <br/>}< br/> else if (contentshown = 2) <br/>{< br/> MessageBox. confirm (this, "prompt", "Are you sure you want to disable this service? ", (S2, E2) => <br/>{< br/> AM. restartpackage (listrunningservices [E. position]. service. packagename); <br/> btngetservice. optional mclick (); <br/>}, (S3, E3) =>{}); <br/>}< br/> }; <br/>}< br/> 

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.