main.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="fill_parent" android:gravity="center_vertical" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="50dip" android:layout_marginLeft="10dip" android:orientation="horizontal" > <TextView android:layout_width="120dip" android:layout_height="wrap_content" android:text="系统可用内存为:" /> <TextView android:id="@+id/tvAvailMemory" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFD700" /> </LinearLayout> <Button android:id="@+id/btProcessInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="查看系统进程信息" /> </LinearLayout>
browse_process_list.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="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/tvTotalProcessNo" android:layout_width="fill_parent" android:layout_height="30dip" android:textSize="20sp" /> <ListView android:id="@+id/listviewProcess" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="10dip" /> </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 android:id="@+id/tvProcessPID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="3dip" android:textColor="#FFD700" /> </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 android:id="@+id/tvProcessUID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFD700" /> </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 android:id="@+id/tvProcessMemSize" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFD700" /> </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 android:id="@+id/tvProcessName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFD700" /> </LinearLayout> </LinearLayout>
ProcessInfo.java
package com.qin.ammp; //Model类 public class ProcessInfo { // 进程id Android规定android.system.uid=1000 private int pid; // 进程所在的用户id ,即该进程是有谁启动的 root/普通用户等 private int uid; // 进程占用的内存大小,单位为kb private int memSize; // 进程名 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 setPocessName(String processName) { this.processName = processName; } }
MainActivity.java
package com.qin.ammp; import android.app.Activity; import android.app.ActivityManager; import android.app.ActivityManager.MemoryInfo; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.text.format.Formatter; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private static String TAG = "AM_MEMORYIPROCESS" ; private ActivityManager mActivityManager = null ; private TextView tvAvailMem ; private Button btProcessInfo ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tvAvailMem = (TextView)findViewById(R.id.tvAvailMemory) ; btProcessInfo =(Button)findViewById(R.id.btProcessInfo); //跳转到显示进程信息界面 btProcessInfo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,BrowseProcessInfoActivity.class); startActivity(intent); } }); //获得ActivityManager服务的对象 mActivityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); //获得可用内存信息 String availMemStr = getSystemAvaialbeMemorySize(); Log.i(TAG, "The Availabel Memory Size is"+availMemStr); //显示 tvAvailMem.setText(availMemStr); } //获得系统可用内存信息 private String getSystemAvaialbeMemorySize(){ //获得MemoryInfo对象 MemoryInfo memoryInfo = new MemoryInfo() ; //获得系统可用内存,保存在MemoryInfo对象上 mActivityManager.getMemoryInfo(memoryInfo) ; long memSize = memoryInfo.availMem ; //字符类型转换 String availMemStr = formateFileSize(memSize); return availMemStr ; } //调用系统函数,字符串转换 long -String KB/MB private String formateFileSize(long size){ return Formatter.formatFileSize(MainActivity.this, size); } }
BrowseProcessInfoActivity.java
package com.qin.ammp; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.app.ActivityManager; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.os.Debug; import android.util.Log; import android.view.ContextMenu; import android.view.MenuItem; import android.view.View; import android.view.ContextMenu.ContextMenuInfo; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; public class BrowseProcessInfoActivity extends Activity implements OnItemClickListener{ private static String TAG = "ProcessInfo"; private static final int KILL_PORCESS = 1; private static final int SEARCH_RUNNING_APP = 2; private ActivityManager mActivityManager = null; // ProcessInfo Model类 用来保存所有进程信息 private List<ProcessInfo> processInfoList = null; private ListView listviewProcess; private TextView tvTotalProcessNo ; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.browse_process_list); listviewProcess = (ListView) findViewById(R.id.listviewProcess); listviewProcess.setOnItemClickListener(this); tvTotalProcessNo =(TextView)findViewById(R.id.tvTotalProcessNo); this.registerForContextMenu(listviewProcess); // 获得ActivityManager服务的对象 mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); // 获得系统进程信息 getRunningAppProcessInfo(); // 为ListView构建适配器对象 BrowseProcessInfoAdapter mprocessInfoAdapter = new BrowseProcessInfoAdapter( this, processInfoList); listviewProcess.setAdapter(mprocessInfoAdapter); tvTotalProcessNo.setText("当前系统进程共有:"+processInfoList.size()); } //杀死该进程,并且刷新 @Override public void onItemClick(AdapterView<?> arg0, View arg1, final int position, long arg3) { // TODO Auto-generated method stub new AlertDialog.Builder(this).setMessage("是否杀死该进程") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub //杀死该进程,释放进程占用的空间 mActivityManager.killBackgroundProcesses(processInfoList.get(position).getProcessName()); //刷新界面 getRunningAppProcessInfo() ; BrowseProcessInfoAdapter mprocessInfoAdapter = new BrowseProcessInfoAdapter( BrowseProcessInfoActivity.this, processInfoList); listviewProcess.setAdapter(mprocessInfoAdapter); tvTotalProcessNo.setText("当前系统进程共有:"+processInfoList.size()); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.cancel() ; } }).create().show() ; } /** * 获得系统进程信息 */ 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; Log.i(TAG, "processName: " + processName + " pid: " + pid + " uid:" + uid + " memorySize is -->" + memSize + "kb"); // 构造一个ProcessInfo对象 ProcessInfo processInfo = new ProcessInfo(); processInfo.setPid(pid); processInfo.setUid(uid); processInfo.setMemSize(memSize); processInfo.setPocessName(processName); processInfoList.add(processInfo); // 获得每个进程里运行的应用程序(包),即每个应用程序的包名 String[] packageList = appProcessInfo.pkgList; Log.i(TAG, "process id is " + pid + "has " + packageList.length); for (String pkg : packageList) { Log.i(TAG, "packageName " + pkg + " in process id is -->" + pid); } } } public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.add(0, 0, KILL_PORCESS, "杀死该进程"); menu.add(0, 0, SEARCH_RUNNING_APP, "运行在该进程的应用程序"); super.onCreateContextMenu(menu, v, menuInfo); } public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case KILL_PORCESS: // 杀死该进程 , 重新加载界面 new AlertDialog.Builder(this).setMessage("是否杀死该进程") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.cancel() ; } }).create().show() ; break; case SEARCH_RUNNING_APP: // 查看运行在该进程的应用程序信息 break; default: break; } return super.onContextItemSelected(item); } }
BrowseProcessInfoAdapter.java
package com.qin.ammp; 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 BrowseProcessInfoAdapter extends BaseAdapter { private List<ProcessInfo> mlistProcessInfo = null; LayoutInflater infater = null; public BrowseProcessInfoAdapter(Context context, List<ProcessInfo> apps) { infater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mlistProcessInfo = apps ; } @Override public int getCount() { System.out.println("size" + mlistProcessInfo.size()); return mlistProcessInfo.size(); } @Override public Object getItem(int position) { return mlistProcessInfo.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertview, ViewGroup arg2) { System.out.println("getView at " + position); View view = null; ViewHolder holder = null; if (convertview == null || convertview.getTag() == null) { view = infater.inflate(R.layout.browse_process_item, null); holder = new ViewHolder(view); view.setTag(holder); } else{ view = convertview ; holder = (ViewHolder) convertview.getTag() ; } 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()); 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) ; } } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.qin.ammp" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".BrowseProcessInfoActivity"></activity> </application> <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" /> </manifest>