用ListView显示当前手机进程信息

——————–直接上代码———————–

   package com.dingxiaomiao.as.activitymanagerdemo;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Bundle;
import android.os.Debug;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


   public class MainActivity extends Activity {
    private ListView listView;
       private List runningAppProcessInfoList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView)findViewById(R.id.listView);
        getRunningAppProcessInfo(this);

    }
       private void getRunningAppProcessInfo(Context context){
           ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
           runningAppProcessInfoList = am.getRunningAppProcesses();
           List> values = new ArrayList<>();
           for(int i = 0;iint pid = info.pid;//pid
               int uid = info.uid;//uid
               String processName = info.processName;//name
               int[] memoryPid = new int[]{pid};
               Debug.MemoryInfo [] memoryInfo = am.getProcessMemoryInfo(memoryPid);
               int size = memoryInfo[0].getTotalPss();//mem
               Map map = new HashMap<>();
               map.put("pid",pid);
               map.put("uid",uid);
               map.put("name",processName);
               map.put("men",size);
                values.add(map);
           }
           SimpleAdapter adapter = new SimpleAdapter(context,values,R.layout.simplelayout,
                   new String[]{"pid","name","uid","men"},//这个顺序才是决定排版的显示顺序
                   new int[]{R.id.pid,R.id.uid,R.id.mem,R.id.name}
           );
           listView.setAdapter(adapter);
           listView.deferNotifyDataSetChanged();

       }
}

你可能感兴趣的:(android)