How to implement task Manager with Android programming _android

Source: Internet
Author: User

An example of this article describes the Android programming implementation of the Task Manager method. Share to everyone for your reference, specific as follows:
The features that Task Manager can implement are:

1. View all processes running under the current system
2. You can view the process number, version number, and memory footprint of each process
3. Kill the process (can kill the whole process or kill the specified process)
4. View system remaining memory

Effect Chart:

Kill All Processes

Realize the idea:

The Activitymanager class can acquire all the processes of the current system, as well as information about each process, and can also kill a process.
Activitymanager.getrunningappprocesses (); Get all processes of the system
Activitymanager.getprocessmemoryinfo (PIDs); Gets the memory footprint of each process based on the process number
Activitymanager.restartpackage (ProcessName); Kill a process based on the package name of the process
Packagemanager can get the program's icon, version number, name, permission, signature according to the package name

Core code:


 * * Get all processes and user processes *
 *
protected void InitData () {
  Runningappprocessinfos = Activitymanager.getrunningappprocesses (); Get all processes
  Userrunningappprocessinfos = new arraylist<activitymanager.runningappprocessinfo> ();//Get 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 of the user's processes, and the system process cannot be killed, so here's to get all of the user's processes.

 * * Get memory information 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 footprint" + memorysize + "KB"); 
public class TaskInfo {context context;
  Packagemanager pm;
    Public TaskInfo {this.context = context;
  PM = Context.getpackagemanager (); * * * * According to package name query icon/public drawable Getappicon (String packname) {try {applicationinfo info = Pm.geta
       Pplicationinfo (packname, 0);
    Return Info.loadicon (PM);
      catch (Namenotfoundexception e) {//TODO auto-generated catch block E.printstacktrace ();
    return null; }/* * Get Program Version number */public string getappversion (string packname) {try {PackageInfo packinfo = PM
       . Getpackageinfo (packname, 0);
      return packinfo.versionname;
        catch (Namenotfoundexception e) {e.printstacktrace ();
      return null; }/* * Get the name of the program */public String getappname (String packname) {try {applicationinfo info = pm.g
         Etapplicationinfo (packname, 0);
      Return Info.loadlabel (PM). toString (); catch (NamenotfounDexception e) {//TODO auto-generated catch block E.printstacktrace ();
      return null; }/* * Get Program Permissions/public string[] Getapppremission (String packname) {try {PackageInfo Packinfo
       = Pm.getpackageinfo (Packname, packagemanager.get_permissions);
      Get all the permissions to return packinfo.requestedpermissions;
        catch (Namenotfoundexception e) {e.printstacktrace ();
      return null;  }/* * Get the signature of the program/public String getappsignature (string packname) {try {PackageInfo packinfo =
       Pm.getpackageinfo (Packname, packagemanager.get_signatures);
      Get all the permissions to 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 interface Pd.show ();
      New Thread () {@Override public void run () {initdata ();
      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) {///If the user is also selected return TRUE//terminate an activity requires the use of Activitymanager activitymanager.restartpackage (Userrunningappprocessi
    Nfos.get (i). ProcessName);
  }//Update listview display Pd.show ();
      New Thread () {@Override public void run () {initdata ();
      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";

I hope this article will help you with the Android program.

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.