Android Task Manager

Source: Internet
Author: User

The task manager can implement the following functions:

1. view all processes running in the current system

2. You can view the process number, version number, and memory usage of each process.

3. Kill the process (you can kill all the processes or kill the specified process)

4. view the remaining memory of the system

: Kill all processes

Implementation ideas:

The activitymanager class can obtain all the processes in the current system and the information of each process. It can also kill a process,

Activitymanager. getrunningappprocesses (); obtain all processes of the system

Activitymanager. getprocessmemoryinfo (PIDS); obtains the memory usage of each process based on the process number.

Activitymanager. restartpackage (processname); kill a process based on its package name

Packagemanager can obtain the program icons, version numbers, names, permissions, and signatures based on the package name.

Core code:

/** Obtain all processes and users' processes */protected void initdata () {runningappprocessinfos = activitymanager. getrunningappprocesses (); // obtain all processes userrunningappprocessinfos = new arraylist <activitymanager. runningappprocessinfo> (); // obtain the user's process for (INT I = 0; I <runningappprocessinfos. size (); I ++) {If ("system ". equals (runningappprocessinfos. get (I ). processname) | "android. process. media ". equals (runningappprocessinfos. get (I ). processname) | "android. process. acore ". equals (runningappprocessinfos. get (I ). processname) {continue;} userrunningappprocessinfos. add (runningappprocessinfos. get (I ));}}

Note: killing all processes actually kills all the processes of the user, and the system process cannot be killed. Therefore, we need to obtain all the processes of the user.

/** Get the memory occupied by a process */activitymanager AM = (activitymanager) getsystemservice (context. activity_service); int [] PIDs = {userrunningappprocessinfos. get (position ). PID}; android. OS. debug. memoryinfo [] memoryinfos = aM. getprocessmemoryinfo (PIDS); int memorysize = memoryinfos [0]. gettotalprivatedirty (); TV _app_memory.settext ("memory usage:" + memorysize + "kb ");

Public class taskinfo {context; packagemanager PM; Public taskinfo (context) {This. context = context; PM = context. getpackagemanager ();}/** query icon by package name */Public drawable getappicon (string packname) {try {applicationinfo info = PM. getapplicationinfo (packname, 0); Return info. loadicon (PM);} catch (namenotfoundexception e) {// todo auto-generated catch blocke. printstacktrace (); return NULL ;}/ ** get the program version */Public String getappversion (string packname) {try {packageinfo packinfo = PM. getpackageinfo (packname, 0); Return packinfo. versionname;} catch (namenotfoundexception e) {e. printstacktrace (); return NULL ;}/ ** get the program name */Public String getappname (string packname) {try {applicationinfo info = PM. getapplicationinfo (packname, 0); Return info. loadlabel (PM ). tostring ();} catch (namenotfoundexception e) {// todo auto-generated catch blocke. printstacktrace (); return NULL ;}/ ** get program permissions */Public String [] getapppremission (string packname) {try {packageinfo packinfo = PM. getpackageinfo (packname, packagemanager. get_permissions); // get all permissions return packinfo. requestedpermissions;} catch (namenotfoundexception e) {e. printstacktrace (); return NULL ;}/ ** obtain the program signature */Public String getappsignature (string packname) {try {packageinfo packinfo = PM. getpackageinfo (packname, packagemanager. get_signatures); // get all permissions return packinfo. signatures [0]. tocharsstring ();} catch (namenotfoundexception e) {e. printstacktrace (); return NULL ;}}}

/** Kill all processes */private void killall () {for (INT I = 0; I <userrunningappprocessinfos. size (); I ++) {system. out. println (userrunningappprocessinfos. get (I ). processname); activitymanager. restartpackage (userrunningappprocessinfos. get (I ). processname);} // update the displayed page PD. show (); New thread () {@ overridepublic void run () {initdata (); message MSG = new message (); MSG. what = get_running_app_finish; hander. sendmessage (MSG );}}. start ();}/** kill selected process */private void killselected () {for (INT I = 0; I <isselected. size (); I ++) {If (isselected. get (I) {// return true if the user is also selected // activitymanageractivitymanager is required to terminate an activity. restartpackage (userrunningappprocessinfos. get (I ). processname) ;}/// update the display PD of listview. show (); New thread () {@ overridepublic void run () {initdata (); message MSG = new message (); MSG. what = get_running_app_finish; hander. sendmessage (MSG );}}. start ();}
/** Get the remaining memory of the system */activitymanager AM = (activitymanager) getsystemservice (context. activity_service); memoryinfo outinfo = new activitymanager. memoryinfo (); AM. getmemoryinfo (outinfo); long availmemorysize = outinfo. availmem; string strmemorysize = availmemorysize/1024/1024 + "MB ";

Do you want to achieve your goals and dreams quickly? Please add QQ: 673220883 every day provides you with a lot of inspirational, video, sales, management, and other-classic articles, free to learn. This QQ space has changed the fate of tens of millions of people. Chen anzhi and you start your dream journey! National Course Registration Hotline: 131
6187 6870

 

Wonderful Sharing: if you have no goals, you will be confused about the future.

Wonderful Sharing: Yu hongmin: only one thing in my life
Don't think too much

Wonderful Sharing: Man's one hundred secrets are impossible to know without saying it!

Wonderful Sharing: Do not make these regrets the 25 most regrettable events before your death
It's your pity

Wonderful Sharing: six steps will help you break the chat embarrassment. We recommend you read the wonderful sharing:
Tell yourself that I can lose, but I will never give up on sharing highlights: business experts do not want others to know about the secrets of communication. Wonderful Sharing: Chen anzhi teaches you how to earn 5 million by the age of thirty, it may take a few minutes for both men and women to read the story of Steve Jobs! Stanford University speech: delay equals death-a course that changes ten millions of lives! Wonderful Sharing: Why are you a talented poor? Articles that benefit you for life! Wonderful Sharing: Life is like a cup of tea. It won't cry for a lifetime, but it will always cry for a while!

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.