马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
进程管理模块
进程管理的话,也是比较简单的,首先是写一个获取手机正在运行的进程的类,此类中会有区分那个是用户进程哪个是系统进程的方法: package cn.cbd.mobilesafe.activity.engine;
import java.util.ArrayList;
import java.util.List;
import cn.cbd.mobilesafe.activity.R;
import cn.cbd.mobilesafe.activity.bean.TaskInfo;
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.os.Debug.MemoryInfo;
public class TaskInfoProvider {
/**
* 获取手机中正在运行的进程信息
* @param context 上下文
* @return 进程信息的集合
*/
public static List<TaskInfo> getRunningTaskInfos(Context context)
{
/**
* 通过ActivityManager获得正在运行的程序的进程
*/
ActivityManager manager=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcessInfos=manager.getRunningAppProcesses();
List<TaskInfo> taskInfos=new ArrayList<TaskInfo>();
PackageManager pm=context.getPackageManager();
/**
* 循环此集合
*/
for(RunningAppProcessInfo info : appProcessInfos)
{
TaskInfo taskInfo=new TaskInfo();
String packageName=info.processName; //得到进程的名字
taskInfo.setPackageName(packageName);
try
{
/**
* 通过进程的名字得到包的信息--PackageInfo
* 这样的话就可以得到程序的图标与标签了
*/
PackageInfo packageInfo=pm.getPackageInfo(packageName, 0);
taskInfo.setIcon(packageInfo.applicationInfo.loadIcon(pm));
taskInfo.setName(packageInfo.applicationInfo.loadLabel(pm).toString());
taskInfo.setUserTask(isUserApp(packageInfo.applicationInfo));
}
catch (NameNotFoundException e)
{
e.printStackTrace();
//有些进程没有图标,没有名子
taskInfo.setIcon(context.getResources().getDrawable(R.drawable.ic_launcher));
taskInfo.setName(packageName);
}
int pid=info.pid;
MemoryInfo[] memoryInfo=manager.getProcessMemoryInfo(new int[]{pid});
taskInfo.setMemorySize(memoryInfo[0].getTotalPrivateDirty()*1024);
taskInfos.add(taskInfo);
}
return taskInfos;
}
/**
* 判断该应用程序是否为用户自定义安装程序
* @param applicationInfo 当前应用的包信息对象
* @return true表示用户程序,false表示系统程序
*/
public static boolean isUserApp(ApplicationInfo applicationInfo)
{
if((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)==1)
{
return false;
}else{
return true;
}
}
}
然后再写一个类来获取正在运行程序的数量,手机剩余的内存,还有手机的RAM的大小: import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
public class ProcessUtil {
/**
* 获取Android手机中正在运行的程序的数量
* @param context 上下文
* @return 正在系统中运行程序的数量
*/
public static int getRunningProcessCount(Context context)
{
ActivityManager manager=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
return manager.getRunningAppProcesses().size();
}
/**
* 获取当前手机的剩余内存
* @param context 上下文
* @return 剩余内存数
*/
public static long getAvailableMemory(Context context)
{
ActivityManager manager=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo menoryInfo=new MemoryInfo();
manager.getMemoryInfo(menoryInfo);
return menoryInfo.availMem;
}
/**
* 获取手机的RAM的大小
* @return
*/
public static long getTotalRamSize()
{
try
{
File file=new File("/proc/meminfo");
FileInputStream fis=new FileInputStream(file);
BufferedReader br=new BufferedReader(new InputStreamReader(fis));
String result=br.readLine();
StringBuffer sb=new StringBuffer();
for(char c : result.toCharArray())
{
if(c>='0' && c<='9')
{
sb.append(c);
}
}
return Integer.parseInt(sb.toString())*1024;
}
catch (IOException e
)
{
e.printStackTrace();
return 0;
}
}
************************************************************************************************************
然后就是加载界面的代码了:
package cn.cbd.mobilesafe.activity;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.cbd.mobilesafe.activity.bean.TaskInfo;
import cn.cbd.mobilesafe.activity.engine.TaskInfoProvider;
import cn.cbd.mobilesafe.activity.util.ProcessUtil;
public class TaskManagerActivity extends Activity {
private TextView tv_task_manager_process_count;
private TextView tv_task_memory_info;
private ListView lv_task_manager;
private LinearLayout ll_task_loading;
private long availaleMemory;
private List<TaskInfo> taskInfos;
private List<TaskInfo> userTaskInfos;
private List<TaskInfo> systemTaskInfos;
private MyAdapter adapter;
/*手机中正在运行的进程数*/
private int runningProcessCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_task_manager);
userTaskInfos=new ArrayList<TaskInfo>();
systemTaskInfos=new ArrayList<TaskInfo>();
tv_task_manager_process_count=(TextView) findViewById(R.id.tv_task_manager_process_count);
tv_task_memory_info=(TextView) findViewById(R.id.tv_task_memory_info);
ll_task_loading=(LinearLayout) findViewById(R.id.ll_task_loading);
availaleMemory=ProcessUtil.getAvailableMemory(this);
runningProcessCount=ProcessUtil.getRunningProcessCount(this);
tv_task_manager_process_count.setText("正在运行的进程:"+runningProcessCount+"个");
tv_task_memory_info.setText("剩余/总内存:"+Formatter.formatFileSize(this,availaleMemory)+"/"+Formatter.formatFileSize(this,ProcessUtil.getTotalRamSize()));
lv_task_manager=(ListView) findViewById(R.id.lv_task_manager);
loadingData();
lv_task_manager.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
CheckBox cb_checked=(CheckBox) view.findViewById(R.id.cb_taskchecked_status);
/**
* TaskInfo是一个javaBean里面有:
* private Drawable icon;
private String name;
private String packageName;
private boolean isUserTask;
private boolean checked;
private long memorySize;
*/
TaskInfo taskInfo=(TaskInfo) lv_task_manager.getItemAtPosition(position);
//如果选中的条目不是当前应用就设置多先框的值
if(!taskInfo.getPackageName().equals(getPackageName()))
{
if(taskInfo.isChecked())
{
taskInfo.setChecked(false);
cb_checked.setChecked(false);
}else{
taskInfo.setChecked(true);
cb_checked.setChecked(true);
}
}
}
});
}
public void loadingData()
{
new AsyncTask<Void, Void, Void>(){
/**
* 这里用到了异步任务
*/
@Override
protected void onPreExecute() {
ll_task_loading.setVisibility(View.VISIBLE);
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
/**
* 此方法的作用就是判断如果是用户进程的话那就把它放在用户进程的集合里面
* 否则就放在系统进程的集合里面
*/
taskInfos=TaskInfoProvider.getRunningTaskInfos(getApplicationContext());
for(TaskInfo taskInfo : taskInfos)
{
if(taskInfo.isUserTask())
{
userTaskInfos.add(taskInfo);
}
else
{
systemTaskInfos.add(taskInfo);
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
/**
* 最后的时候实例化适配器
* 添加适配器到listView里面
*/
ll_task_loading.setVisibility(View.INVISIBLE);
adapter=new MyAdapter();
lv_task_manager.setAdapter(adapter);
super.onPostExecute(result);
}
}.execute();
}
/**
* 刹死选中的进程
* @param v
*/
public void killProcess(View v)
{
List<TaskInfo> killProcessTaskInfos=new ArrayList<TaskInfo>();
int count=0;
long memory=0;
ActivityManager manager=(ActivityManager) getSystemService(ACTIVITY_SERVICE);
for(TaskInfo taskInfo : userTaskInfos)
{
if(taskInfo.isChecked())
{
count++;
memory+=taskInfo.getMemorySize();
manager.killBackgroundProcesses(taskInfo.getPackageName());
killProcessTaskInfos.add(taskInfo);
}
}
for(TaskInfo taskInfo : systemTaskInfos)
{
if(taskInfo.isChecked())
{
count++;
memory+=taskInfo.getMemorySize();
manager.killBackgroundProcesses(taskInfo.getPackageName());
killProcessTaskInfos.add(taskInfo);
}
}
Toast.makeText(this, "清理掉了"+count+"个进程\n释放了"+Formatter.formatFileSize(this, memory)+"空间", Toast.LENGTH_SHORT).show();
availaleMemory+=memory;
runningProcessCount-=count;
tv_task_manager_process_count.setText("正在运行的进程:"+runningProcessCount+"个");
tv_task_memory_info.setText("剩余/总内存:"+Formatter.formatFileSize(this,availaleMemory)+"/"+Formatter.formatFileSize(this,ProcessUtil.getTotalRamSize()));
//从集合中删除被刹死的进程
for(TaskInfo taskInfo : killProcessTaskInfos)
{
if(taskInfo.isUserTask())
{
userTaskInfos.remove(taskInfo);
}else{
systemTaskInfos.remove(taskInfo);
}
}
//通知数据适配器更新数据
adapter.notifyDataSetChanged();
}
private class MyAdapter extends BaseAdapter
{
/**
* 此类是查询列表的适配器
* 关于此类中的方法的话,在程序管理的那个模块说的很清楚了
* 这里的话就不一一细说了
* @author Administrator
*
*/
@Override
public boolean isEnabled(int position) { //判断位置是否是分隔栏的位置
if(position==0 || position==userTaskInfos.size()+1)
{
return false;
}
else
{
return true;
}
}
@Override
public int getCount() {
/**
* 此方法的话有必要说明一下
* 是通过共享数据来对用户进程与系统进程的数量进行控制
* 如果showSystem是true的话那就是系统进程就返回的是系统进程的数量
* 否则返回就是用户程序的数量
*/
SharedPreferences sharedPreferences=getSharedPreferences("config",Context.MODE_PRIVATE);
boolean showSystem=sharedPreferences.getBoolean("showSystemProcess", false);
if(showSystem){
return userTaskInfos.size()+systemTaskInfos.size()+2;
}else{
return userTaskInfos.size()+1;
}
}
@Override
public Object getItem(int position) {
/**
* 判断返回的条目是用户进程的还是系统进程的
*/
if(position==0)
{
return null;
}else if(position<=userTaskInfos.size()){
return userTaskInfos.get(position-1);
}else if(position==userTaskInfos.size()+1){
return null;
}else{
return systemTaskInfos.get(position-userTaskInfos.size()-2);
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(position==0)
{
TextView tv=new TextView(getApplicationContext());
tv.setTextColor(Color.BLACK);
tv.setBackgroundColor(Color.GRAY);
tv.setText("用户进程:"+userTaskInfos.size()+"个");
return tv;
}else if(position<=userTaskInfos.size()){
View view=View.inflate(getApplicationContext(), R.layout.layout_task_item, null);
ImageView iv_icon=(ImageView) view.findViewById(R.id.iv_task_icon);
TextView tv_name=(TextView)view.findViewById(R.id.tv_task_name);
TextView tv_memory=(TextView)view.findViewById(R.id.tv_task_memory);
CheckBox cb_checked=(CheckBox) view.findViewById(R.id.cb_taskchecked_status);
TaskInfo taskInfo=userTaskInfos.get(position-1);
if(taskInfo.getPackageName().equals(getPackageName()))
{
cb_checked.setVisibility(View.INVISIBLE);
}
iv_icon.setImageDrawable(taskInfo.getIcon());
tv_name.setText(taskInfo.getName());
tv_memory.setText("占用内存:"+Formatter.formatFileSize(getApplicationContext(), taskInfo.getMemorySize()));
cb_checked.setChecked(taskInfo.isChecked());
return view;
}else if(position==userTaskInfos.size()+1){
TextView tv=new TextView(getApplicationContext());
tv.setTextColor(Color.BLACK);
tv.setBackgroundColor(Color.GRAY);
tv.setText("系统进程:"+systemTaskInfos.size()+"个");
return tv;
}else{
View view=View.inflate(getApplicationContext(), R.layout.layout_task_item, null);
ImageView iv_icon=(ImageView) view.findViewById(R.id.iv_task_icon);
TextView tv_name=(TextView)view.findViewById(R.id.tv_task_name);
TextView tv_memory=(TextView)view.findViewById(R.id.tv_task_memory);
CheckBox cb_checked=(CheckBox) view.findViewById(R.id.cb_taskchecked_status);
TaskInfo taskInfo=systemTaskInfos.get(position-userTaskInfos.size()-2);
iv_icon.setImageDrawable(taskInfo.getIcon());
tv_name.setText(taskInfo.getName());
tv_memory.setText("占用内存:"+Formatter.formatFileSize(getApplicationContext(), taskInfo.getMemorySize()));
cb_checked.setChecked(taskInfo.isChecked());
return view;
}
}
}
}
|