<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- 定义获取可用内存的按钮 --> <Button android:id="@+id/btn_main_ablememory" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="可用 的内存"/> <!-- 定义查看正在运行的进程的按钮 --> <Button android:id="@+id/btn_main_lookruningmemory" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="查看正在运行的进程"/> <!-- 定义显示当前进程数的标签控件 --> <TextView android:id="@+id/tv_main_currentprocessnum" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <!-- 定义进程列表控件 --> <ListView android:id="@+id/lv_main_list" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <!-- 定义获取内容显示的标签控件 --> <TextView android:id="@+id/tv_main_text" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
browse_process_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="80dip" android:orientation="vertical"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:orientation="horizontal"> <TextView android:layout_width="100dip" android:layout_height="wrap_content" android:text="进程ID : "></TextView> <TextView android:id="@+id/tvProcessPID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="3dip" android:textColor="#FFD700"></TextView> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:orientation="horizontal"> <TextView android:layout_width="100dip" android:layout_height="wrap_content" android:text="进程所在UID:"></TextView> <TextView android:id="@+id/tvProcessUID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFD700"></TextView> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:orientation="horizontal"> <TextView android:layout_width="100dip" android:layout_height="wrap_content" android:text="进程占用内存:"></TextView> <TextView android:id="@+id/tvProcessMemSize" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFD700"></TextView> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:orientation="horizontal"> <TextView android:layout_width="100dip" android:layout_height="wrap_content" android:text="进程名:"></TextView> <TextView android:id="@+id/tvProcessName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFD700"></TextView> </LinearLayout> </LinearLayout>
package com.example.yanlei.yl2; import android.app.ActivityManager; import android.app.ActivityManager.MemoryInfo; import android.content.Context; import android.os.Bundle; import android.os.Debug; import android.support.v7.app.AppCompatActivity; import android.text.format.Formatter; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { // 得到可用内存的按钮对象 private Button btnEnableMember; // 查看运行的程序 private Button btnLookRuningMemory; // 得到activity的管理对象 private ActivityManager mActivityManager; // 显示可用内存的标签 private TextView tvText; // 显示进程的列表对象 private ListView listviewProcess; // 进程数组对象 private ArrayList<ProcessInfo> processInfoList; // 显示进程总数的对象 private TextView tvCurrentProcessNum; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 得到布局中的控件 findView(); // 给控件设置监听器 setListener(); // 得到ActivityManager服务 mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); } private void setListener() { // TODO Auto-generated method stub btnEnableMember.setOnClickListener(listener); btnLookRuningMemory.setOnClickListener(listener); } private void findView() { // 得到布局中的控件对象 btnEnableMember = (Button) findViewById(R.id.btn_main_ablememory); btnLookRuningMemory = (Button) findViewById(R.id.btn_main_lookruningmemory); tvText = (TextView) findViewById(R.id.tv_main_text); listviewProcess = (ListView) findViewById(R.id.lv_main_list); tvCurrentProcessNum = (TextView) findViewById(R.id.tv_main_currentprocessnum); } OnClickListener listener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn_main_ablememory: // 得到系统可用的内存 String availMemStr = getSystemAvaialbeMemorySize(); // 设置textview的内容 tvText.setText(availMemStr); // 设置textview可见 tvText.setVisibility(View.VISIBLE); // 设置listview不可见 listviewProcess.setVisibility(View.GONE); break; case R.id.btn_main_lookruningmemory: // 设置textview不可见 tvText.setVisibility(View.GONE); // 设置listview可见 listviewProcess.setVisibility(View.VISIBLE); // 获得系统进程信息 getRunningAppProcessInfo(); // 向适配器中放置数据源 setAdapter(); break; default: break; } } }; protected String getSystemAvaialbeMemorySize() { // TODO Auto-generated method stub // 获得MemoryInfo对象 MemoryInfo memoryInfo = new MemoryInfo(); // 获得系统可用内存,保存在MemoryInfo对象上 mActivityManager.getMemoryInfo(memoryInfo); long memSize = memoryInfo.availMem; // 字符类型转换 String availMemStr = formateFileSize(memSize); return availMemStr; } // 字符类型转换 private String formateFileSize(long size) { return Formatter.formatFileSize(MainActivity.this, size); } // 向适配器中放置数据源 protected void setAdapter() { // TODO Auto-generated method stub ProcessInfoAdapter mprocessInfoAdapter = new ProcessInfoAdapter(this, processInfoList); listviewProcess.setAdapter(mprocessInfoAdapter); } // 获得系统进程信息 private void getRunningAppProcessInfo() { // ProcessInfo Model类 用来保存所有进程信息 processInfoList = new ArrayList<ProcessInfo>(); // 通过调用ActivityManager的getRunningAppProcesses()方法获得系统里所有正在运行的进程 List<ActivityManager.RunningAppProcessInfo> appProcessList = mActivityManager .getRunningAppProcesses(); for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessList) { // 进程ID号 int pid = appProcessInfo.pid; // 用户ID 类似于Linux的权限不同,ID也就不同 比如 root等 int uid = appProcessInfo.uid; // 进程名,默认是包名或者由属性android:process=""指定 String processName = appProcessInfo.processName; // 获得该进程占用的内存 int[] myMempid = new int[] { pid }; // 此MemoryInfo位于android.os.Debug.MemoryInfo包中,用来统计进程的内存信息 Debug.MemoryInfo[] memoryInfo = mActivityManager .getProcessMemoryInfo(myMempid); // 获取进程占内存用信息 kb单位 int memSize = memoryInfo[0].dalvikPrivateDirty; // 构造一个ProcessInfo对象 ProcessInfo processInfo = new ProcessInfo(); // 设置processinfo信息 processInfo.setPid(pid); processInfo.setUid(uid); processInfo.setMemSize(memSize); processInfo.setProcessName(processName); // 添加到processInfoList中 processInfoList.add(processInfo); } // 得到进程个数 tvCurrentProcessNum.setText("当前系统进程共有:" + processInfoList.size()); } }
ProcessInfo.java
package com.example.yanlei.yl2; //定义ProcessInfo类,代表一个进程的信息 public class ProcessInfo { private int pid; // 进程id Android规定android.system.uid=1000 private int uid; // 进程所在的用户id ,即该进程是有谁启动的 root/普通用户等 private int memSize; // 进程占用的内存大小,单位为kb private String processName; // 进程名 public ProcessInfo(){} public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public int getMemSize() { return memSize; } public void setMemSize(int memSize) { this.memSize = memSize; } public String getProcessName() { return processName; } public void setProcessName(String processName) { this.processName = processName; } }
ProcessInfoAdapter.java
package com.example.yanlei.yl2; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; //自定义适配器类,提供给listView的自定义view public class ProcessInfoAdapter extends BaseAdapter { // 需要显示的对象列表 private List<ProcessInfo> mlistProcessInfo = null; LayoutInflater infater = null; public ProcessInfoAdapter(Context context, List<ProcessInfo> apps) { // 初始化infater对象 infater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); // 初始化需要显示的mlistProcessInfo对象 mlistProcessInfo = apps; } @Override public int getCount() { // TODO Auto-generated method stub System.out.println("size" + mlistProcessInfo.size()); return mlistProcessInfo.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return mlistProcessInfo.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertview, ViewGroup arg2) { View view = null; ViewHolder holder = null; if (convertview == null || convertview.getTag() == null) { // 通过XML得到VIEW view = infater.inflate(R.layout.browse_process_item, null); holder = new ViewHolder(view); // 把查找的view缓存起来 view.setTag(holder); } else { view = convertview; // 取出缓存的VIEW holder = (ViewHolder) convertview.getTag(); } // 得到ProcessInfo信息,并在item的每个位置显示相关信息 ProcessInfo processInfo = (ProcessInfo) getItem(position); holder.tvPID.setText(processInfo.getPid() + ""); holder.tvUID.setText(processInfo.getUid() + ""); holder.tvProcessMemSize.setText(processInfo.getMemSize() + "KB"); holder.tvProcessName.setText(processInfo.getProcessName()); // 返回需要显示的view return view; } // 占位符 class ViewHolder { TextView tvPID; // 进程ID TextView tvUID; // 用户ID TextView tvProcessMemSize; // 进程占用内存大小 TextView tvProcessName; // 进程名 public ViewHolder(View view) { this.tvPID = (TextView) view.findViewById(R.id.tvProcessPID); this.tvUID = (TextView) view.findViewById(R.id.tvProcessUID); this.tvProcessMemSize = (TextView) view .findViewById(R.id.tvProcessMemSize); this.tvProcessName = (TextView) view .findViewById(R.id.tvProcessName); } } }
时间: 2024-10-13 17:48:13