首页 > 程序开发 > 移动开发 > 其他 >

ListView显示进程信息

2017-04-21

ListView显示进程信息,通过ActivityManager获取进程相关信息,PackageManger获取包的相关信息,创建bean文件TaskInfo java,然后初始化一个个进程bean的对象TaskInfoParser java。

通过ActivityManager获取进程相关信息,PackageManger获取包的相关信息,创建bean文件TaskInfo.java,然后初始化一个个进程bean的对象TaskInfoParser.java。创建布局文件初始化每一个List,然后在逻辑文件中实现显示。

每一个条目的布局文件item_task_manager.xml



    

    
    
        
             
    
    
	   		        
    


主界面activity_task_activity.xml



    

    
    
        
		        
    
    
TaskInfo.java进程的bean文件,封装task中所需要的信息

package com.ldw.safe.bean;

import android.graphics.drawable.Drawable;

/*
 * 进程的bean文件
 */
public class TaskInfo {

	private Drawable icon;
	private String packageName;
	private String appName;
	private long memorySize;
	//是否是用户进程
	private boolean userApp;
	 //判断当前的item的条目是否被勾选上
	private boolean checked;
	public Drawable getIcon() {
		return icon;
	}
	public void setIcon(Drawable icon) {
		this.icon = icon;
	}
	public String getPackageName() {
		return packageName;
	}
	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}
	public String getAppName() {
		return appName;
	}
	public void setAppName(String appName) {
		this.appName = appName;
	}
	public long getMemorySize() {
		return memorySize;
	}
	public void setMemorySize(long memorySize) {
		this.memorySize = memorySize;
	}
	public boolean isUserApp() {
		return userApp;
	}
	public void setUserApp(boolean userApp) {
		this.userApp = userApp;
	}
	public boolean isChecked() {
		return checked;
	}
	public void setChecked(boolean checked) {
		this.checked = checked;
	}
	@Override
	public String toString() {
		return "TaskInfo [icon=" + icon + ", packageName=" + packageName
				+ ", appName=" + appName + ", memorySize=" + memorySize
				+ ", userApp=" + userApp + ", checked=" + checked + "]";
	}
	
}

封装所有进程的bean文件TaskInfoParser.java

package com.ldw.safe.engine;

import java.util.ArrayList;
import java.util.List;

import android.R.integer;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.os.Debug.MemoryInfo;

import com.ldw.safe.R;
import com.ldw.safe.bean.TaskInfo;

/*
 * 进程信息的逻辑文件
 */
public class TaskInfoParser {
	
	public static List getTaskInfos(Context context) {
		
		//PackageManager包管理器
		PackageManager packageManager = context.getPackageManager();
		
		//创建一个集合存储进程相关信息
		List TaskInfos = new ArrayList();
		
		//获取到进程管理器
		ActivityManager activityManager = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
		//获取到手机上面所有运行的进程
		List appProcesses = activityManager.getRunningAppProcesses();
		
		for(RunningAppProcessInfo runningAppProcessInfo:appProcesses){
			//创建一个进程的bean
			TaskInfo taskInfo = new TaskInfo();
			//获取到进程的名字
			String processName = runningAppProcessInfo.processName;
			
			try {
				//获取到进程的包的信息
				PackageInfo packageInfo = packageManager.getPackageInfo(processName, 0);
				//获取到进程的PIDS,只有一个数据
				MemoryInfo[] memoryInfo = activityManager
						.getProcessMemoryInfo(new int[]{runningAppProcessInfo.pid});
				// Dirty弄脏
				// 获取到总共弄脏多少内存(当前应用程序占用多少内存)
				int totalPrivateDirty = memoryInfo[0].getTotalPrivateDirty() * 1024;
				// /获取到图片
				Drawable icon = packageInfo.applicationInfo
						.loadIcon(packageManager);
				// 获取到应用的名字
				String appName = packageInfo.applicationInfo.loadLabel(
						packageManager).toString();
				
				taskInfo.setAppName(appName);
				taskInfo.setMemorySize(totalPrivateDirty);
				taskInfo.setIcon(icon);
				
				
				System.out.println("-------------------");
				System.out.println("processName="+processName);
				System.out.println("appName="+appName);
				
				//获取到当前应用程序的标记
				int flags = packageInfo.applicationInfo.flags;
				//ApplicationInfo.FLAG_SYSTEM 表示系统应用程序
				if((flags & ApplicationInfo.FLAG_SYSTEM) != 0 ){
					//系统应用
					taskInfo.setUserApp(false);
				}else{
//					/用户应用
					taskInfo.setUserApp(true);
					
				}
				
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				//没有图标的额时候设置默认的图标,将app的名字设置成进程名字
				taskInfo.setAppName(processName);
				taskInfo.setIcon(context.getResources().getDrawable(
						R.drawable.ic_launcher));
			}
			TaskInfos.add(taskInfo);
			
		}
		
		return TaskInfos;
	}

}

业务逻辑文件TaskMangerActivity.java

package com.ldw.safe.Activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ldw.safe.R;
import com.ldw.safe.bean.TaskInfo;
import com.ldw.safe.engine.TaskInfoParser;
import com.ldw.safe.utils.SharedPreferencesUtils;
import com.ldw.safe.utils.SystemInfoUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

/*
 * 进程管理
 */
public class TaskManagerActivity extends Activity {

	@ViewInject(R.id.tv_task_process_count)
	private TextView tv_task_process_count;
	@ViewInject(R.id.tv_task_memory)
	private TextView tv_task_memory;
	@ViewInject(R.id.list_view)
	private ListView list_view;
	
	private long availMem;
	private long totalMem;
	private List taskInfos;
	private List userTaskInfos;
	private List systemAppInfos;
	private TaskManagerAdapter adapter;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
        //初始化界面
        initUi();
        initData();
	}
	
	private class TaskManagerAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return taskInfos.size();
		}

		@Override
		public Object getItem(int position) {
			//获取到条目
			if (position == 0) {
				return null;
			} else if (position == userTaskInfos.size() + 1) {
				return null;
			}

			TaskInfo taskInfo;

			if (position < (userTaskInfos.size() + 1)) {
				// 用户程序
				taskInfo = userTaskInfos.get(position - 1);// 多了一个textview的标签 ,
															// 位置需要-1
			} else {
				// 系统程序
				int location = position - 1 - userTaskInfos.size() - 1;
				taskInfo = systemAppInfos.get(location);
			}
			return taskInfo;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			//初始化动作,区分系统进程和第三方进程
			if (position == 0) {
				// 第0个位置显示的应该是 用户程序的个数的标签。
				TextView tv = new TextView(getApplicationContext());
				tv.setBackgroundColor(Color.GRAY);
				tv.setTextColor(Color.WHITE);
				tv.setText("第三方程序:" + userTaskInfos.size() + "个");
				return tv;
			} else if (position == (userTaskInfos.size() + 1)) {
				TextView tv = new TextView(getApplicationContext());
				tv.setBackgroundColor(Color.GRAY);
				tv.setTextColor(Color.WHITE);
				tv.setText("系统程序:" + systemAppInfos.size() + "个");
				return tv;
			}
			
			ViewHolder holder;
			View view;
			
			view = View.inflate(TaskManagerActivity.this, R.layout.item_task_manager, null);
			
			holder = new ViewHolder();
			holder.iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
			holder.tv_name = (TextView) view.findViewById(R.id.tv_name);
			holder.tv_app_memory_size = (TextView) view.findViewById(R.id.tv_app_memory_size);
			holder.tv_app_status = (CheckBox) view.findViewById(R.id.tv_app_status);
			

			TaskInfo taskInfo;
			if (position < (userTaskInfos.size() + 1)) {
				// 第三方程序
				taskInfo = userTaskInfos.get(position - 1);// 多了一个textview的标签 ,
															// 位置需要-1
			} else {
				// 系统程序
				int location = position - 1 - userTaskInfos.size() - 1;
				taskInfo = systemAppInfos.get(location);
			}
			
			//设置图标
			holder.iv_icon.setImageDrawable(taskInfo.getIcon());
			//设置应用的名字
			holder.tv_name.setText(taskInfo.getAppName());
			//设置应用的大小
			holder.tv_app_memory_size.setText("内存占用:"
					+ Formatter.formatFileSize(TaskManagerActivity.this,
							taskInfo.getMemorySize()));
			
			//holder.tv_app_status.se
			
			view.setTag(holder);
			
			
			

			return view;
		}
		
	}
	
	static class ViewHolder{
		ImageView iv_icon;
		TextView tv_name;
		TextView tv_app_memory_size;
		CheckBox tv_app_status;
	}
	
	/*
	private Handler handler = new Handler(){

		public void handlerMessage(android.os.Message msg){
			adapter = new TaskManagerAdapter();
			list_view.setAdapter(adapter);
		};
	};
	*/

	private void initData() {
		new Thread(){

			public void run(){
				taskInfos = TaskInfoParser.getTaskInfos(TaskManagerActivity.this);
				//第三方进程
				userTaskInfos = new ArrayList();
				//系统进程
				systemAppInfos = new ArrayList();
				//初始化区分系统进程和第三方进程
				for (TaskInfo taskInfo : taskInfos) {

					if (taskInfo.isUserApp()) {
						userTaskInfos.add(taskInfo);
					} else {
						systemAppInfos.add(taskInfo);
					}
				}
				//不用发消息的方法显示
				//handler.sendEmptyMessage(0);
				
				runOnUiThread(new Runnable() {

					@Override
					public void run() {
						adapter = new TaskManagerAdapter();
						list_view.setAdapter(adapter);
					}
				});
				
			};
		}.start();
		
	}

	/*
	 * PackageManager包管理器
	 * ActivityManager活动管理器(任务管理器)
	 */
	//初始化界面
	private void initUi() {
		setContentView(R.layout.activity_task_manager);
		ViewUtils.inject(this);
		/*封装起来
		//PackageManager packageManager = getPackageManager();
		//得到进程管理者
		ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		//获取到当前运行的进程
		List runningAppProcesses = activityManager.getRunningAppProcesses();
		//获取当前手机上面一共有多少进程
		int size = runningAppProcesses.size();
		//设置UI
		 * *
		 */
		//获取到总的进程的数据
		int processCount = SystemInfoUtils.getProcessCount(this);
		tv_task_process_count.setText("运行中的进程" + processCount + "个");
		
		//可用内存
		availMem = SystemInfoUtils.getAvailMem(this);
		//总内存
		totalMem = SystemInfoUtils.getTotalMem(this);
		
		tv_task_memory.setText("剩余/总内存:" + 
				Formatter.formatFileSize(TaskManagerActivity.this, availMem) + "/" +
						Formatter.formatFileSize(TaskManagerActivity.this, totalMem));
		
		/*代码整合到工具类中
		MemoryInfo memoryInfo = new MemoryInfo();
		//获取内存信息
		activityManager.getMemoryInfo(memoryInfo);
		
		availMem = memoryInfo.availMem;
		//这个代码不支持低版本手机
		totalMem = memoryInfo.totalMem;

		//手机总内存的文件保存在/proc/meminfo文件中的第一条,读取这个可以获取到总内存
		//这个形式表示MemTotal: 344740 kB
		
		// /proc/meminfo 配置文件的路径
			try {
				//创建一个输入流,配置文件的路径
				FileInputStream fis = new FileInputStream(new File("/proc/meminfo"));
				//读取文件内容
				BufferedReader reader = new BufferedReader(new InputStreamReader(
						fis));
				//读取第一行的信息
				String readLine = reader.readLine();

				StringBuffer sb = new StringBuffer();
				//获取到数字的内容
				for (char c : readLine.toCharArray()) {
					if (c >= &#39;0&#39; && c <= &#39;9&#39;) {
						sb.append(c);
					}
				}
				
				totalMem = Long.parseLong(sb.toString()) * 1024;
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		tv_task_memory.setText("剩余/总内存:" + 
		Formatter.formatFileSize(TaskManagerActivity.this, availMem) + "/" +
				Formatter.formatFileSize(TaskManagerActivity.this, totalMem));
		*/
		
	}
}



相关文章
最新文章
热点推荐